<?php

namespace AutoAdmin\Bundle\Menu\Twig;

use AutoAdmin\Bundle\Menu\Provider\MenuProvider;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class AutoAdminMenuExtension extends AbstractExtension
{
    public function __construct(
        protected MenuProvider $provider,
    ) {
    }

    public function getFunctions(): array
    {
        return [
            new TwigFunction('aa_menu', [$this, 'render'], ['is_safe' => ['html']]),
            new TwigFunction('aa_menu_exists', [$this, 'menuExists'], ['is_safe' => ['html']]),
        ];
    }

    public function render(string $menuName): string
    {
        if (empty($options['currentClass'])) {
            $options['currentClass'] = 'active';
        }

        if (empty($options['quiet_if_not_found'])) {
            $options['quiet_if_not_found'] = true;
        }

        if ($options['quiet_if_not_found'] && !$this->provider->doSlugExist($menuName)) {
            return '<!-- aa:menu:menu-not-found -->';
        }

        return $this->provider->render($menuName);
    }

    public function menuExists(string $menuName): bool
    {
        return $this->provider->doSlugExist($menuName);
    }
}
