<?php

declare(strict_types=1);

namespace KupShop\ComponentsBundle\Twig\Components;

use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ContentBundle\Util\InfoPanelLoader;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Components/components/InfoPanel/InfoPanel.1.html.twig')]
#[Component(2, [
    new Version(1),
    new Version(2, newTemplate: true, newJs: true),
])]
class InfoPanel extends BaseComponent
{
    #[Required]
    public InfoPanelLoader $infoPanelLoader;
    public ?array $infoPanel = null;
    public bool $use_session_storage = false;

    public function getInfoPanel(): ?array
    {
        // TODO: V InfopanelLoader chybí nechutně nahackovaná podpora pro "{DOPRAVA_ZDARMA_OD}", která je v get_infopanel.
        if ($this->infoPanel === null && ($infoPanel = $this->infoPanelLoader->loadInfoPanel())) {
            $this->infoPanel = $infoPanel;
        }

        return $this->infoPanel;
    }

    public function decodeJson(string $json)
    {
        return json_decode($json, true);
    }
}
