<?php

namespace AutoAdmin\Bundle\Admin\Twig\Extension;

use AutoAdmin\Bundle\Content\Context\ContentContext;
use AutoAdmin\Bundle\Content\Site\SiteContextHelper;
use AutoAdmin\Bundle\Content\Translation\LocaleSwitchHelper;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class LocaleExtension extends AbstractExtension
{

    public function __construct(
        protected SiteContextHelper $contextHelper,
        protected LocaleSwitchHelper $localeSwitchHelper,
        protected ContentContext $contentContext,
    ) {
    }

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

    public function getSwitches(?object $content): array
    {
        $this->contentContext->setExtra('admin_main_content', $content);

        $localeSwitch = $this->localeSwitchHelper->buildCurrentSwitch();
        $localeSwitchers = $this->localeSwitchHelper->buildAlternateSwitches();

        return [
            'locale_current_switch' => $localeSwitch,
            'locale_alternate_switches' => $localeSwitchers,
        ];
    }
}
