<?php

namespace AutoAdmin\Bundle\Admin\Twig\Extension;

use AutoAdmin\Bundle\Content\Context\SiteContext;
use Doctrine\Common\Collections\ArrayCollection;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class LocaleExtension extends AbstractExtension
{

    public function __construct(
        protected SiteContext $siteContext
    ) {
    }

    public function getFunctions(): array
    {
        return [
            new TwigFunction('aa_admin_locale_switcher', [$this, 'getSwitcher']),
        ];
    }

    public function getSwitcher(): array
    {
        $currentLocale = $this->siteContext->getLocale();
        /** @var ArrayCollection $locales */
        $locales = $this->siteContext->getDomain()->getLocales();

        return [
            'current' => $this->siteContext->getLocale(),
            'locales' => array_filter($locales->toArray(), function ($el) use ($currentLocale) {
                return $el != $currentLocale;
            }),
        ];
    }
}
