<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\LocalizedRouting\LocalizedPageRouteResolver;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\LocalizedRouting\LocalizedPageRoutes;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class LocalizedPageRouteExtension extends AbstractExtension
{
    public function __construct(
        private readonly LocalizedPageRouteResolver $localizedPageRouteResolver,
    ) {
    }

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

    /**
     * @param array<string, mixed> $parameters
     */
    public function getLocalizedPageRoutes(?string $routeName = null, array $parameters = []): LocalizedPageRoutes
    {
        $currentRoutes = $this->localizedPageRouteResolver->current();
        if (null === $routeName || '' === $routeName) {
            return $currentRoutes;
        }

        return $this->localizedPageRouteResolver->resolve(
            $routeName,
            $parameters,
            $currentRoutes->currentLocale,
        );
    }
}
