<?php

namespace KupShop\ComponentsBundle\EventListener;

use KupShop\ComponentsBundle\Twig\Components\Utils\GTM\Script;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent;

class PreCreateListener implements EventSubscriberInterface
{
    #[Required]
    public Script $gtmScript;

    public static function getSubscribedEvents(): array
    {
        return [
            PreCreateForRenderEvent::class => [
                ['BasicRenderCheck', 100],
            ],
        ];
    }

    public function BasicRenderCheck(PreCreateForRenderEvent $event)
    {
        $dbcfg = \Settings::getDefault();

        switch ($event->getName()) {
            case 'GTM:Script':
                if (!$this->gtmScript->getGTMId()) {
                    $event->setRenderedString(' ');
                }
                // no break
            case 'CookieBar':
                if ($dbcfg->cookie_bar['enable'] != 'Y') {
                    $event->setRenderedString(' ');
                }
        }

        return true;
    }
}
