<?php

namespace KupShop\ComponentsBundle\Twig\Components;

use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ContentBundle\Entity\Page;
use KupShop\ContentBundle\Entity\Wrapper\PageWrapper;
use KupShop\ContentBundle\Util\MenuLinksPage;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Components/components/Menu/Menu.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class Menu extends BaseComponent
{
    public function __construct(
        private readonly MenuLinksPage $menuLinksPage,
        private readonly PageWrapper $pageWrapper,
    ) {
    }

    public string $label;

    public int $level_max = -1;

    private ?Page $page;

    private function getPage(string $label): ?Page
    {
        return $this->page ??= $this->menuLinksPage->getPageWithChildren($label, withBlocks: false);
    }

    public function getMenu(): ?PageWrapper
    {
        if ($this->label && ($page = $this->getPage($this->label))) {
            return $this->pageWrapper->wrap($page);
        }

        return null;
    }
}
