<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CmsBundle\Components;

use Ferienpass\CmsBundle\Routing\LocaleRouteProviderDecorator;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;

#[AsTwigComponent(template: '@Contao/components/language_selector.html.twig')]
class LanguageSelector
{
    private const array NAMES = [
        'de' => 'Deutsch',
        'en' => 'English',
        'nl' => 'Nederlands',
        'ar' => 'العربية',
        'ru' => 'Русский',
        'uk' => 'Українська',
        'tr' => 'Türkçe',
        'pl' => 'Polski',
        'sq' => 'Shqip',
        'ku' => 'Kurmancî',
        'es' => 'Español',
        'fr' => 'Français',
        'pt' => 'Português',
        'sw' => 'Kiswahili',
        'prs' => 'دری',
        'zh' => '中文',
        'sr' => 'Српски',
        'hr' => 'Hrvatski',
        'bs' => 'Bosanski',
    ];

    public function __construct(private readonly RequestStack $requestStack)
    {
    }

    #[ExposeInTemplate]
    public function getOptions(): array
    {
        $request = $this->requestStack->getCurrentRequest();
        if (null === $request) {
            return [];
        }

        $route = $request->attributes->get('_route');
        if (!$route) {
            return [];
        }

        $languages = [];
        foreach (array_keys(LocaleRouteProviderDecorator::MAP) as $locale) {
            $languages[] = [
                'locale' => $locale,
                'name' => self::NAMES[$locale] ?? $locale,
            ];
        }

        return $languages;
    }
}
