<?php

namespace AutoAdmin\Bundle\Admin\Twig\Extension;

use AutoAdmin\Bundle\Content\Site\SiteContextHelper;
use Doctrine\Common\Collections\ArrayCollection;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class LocaleExtension extends AbstractExtension
{

    public function __construct(
        protected SiteContextHelper $contextHelper
    ) {
    }

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

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

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