<?php

declare(strict_types=1);

namespace MyCustomizer\WooCommerce\Connector\Controller;

use MyCustomizer\WooCommerce\Connector\Config\MczrConfig;
use MyCustomizer\WooCommerce\Connector\Factory\MczrFactory;
use MyCustomizer\WooCommerce\Connector\Libs\MczrSettings;

class MczrAssetController
{
    /** @var MczrFactory */
    private $factory;
    /** @var MczrSettings */
    private $settings;
    /** @var \Twig\Environment */
    private $twig;

    public function __construct()
    {
        $this->factory = new MczrFactory();
        $this->settings = new MczrSettings();
        $this->twig = $this->factory->getTwig();
    }

    public function init(): void
    {
        \add_action('wp_head', [$this, 'constants'], 10, 0);
    }

    public function constants(): void
    {
        $vars = [
            'data' => [
                'brand' => $this->settings->get('brand'),
                'customizerBaseUrl' => str_replace('{{brand}}', $this->settings->get('brand'), MczrConfig::getConfig()['customizerUrlPattern']),
            ]
        ];

        $this->settings->getAll();

        $allowed_html = [
            'a' => [
                'href' => [],
                'target' => [],
            ],
            'br' => [],
            'button' => [
                'class' => [],
                'id' => [],
                'name' => [],
                'type' => [],
            ],
            'div' => [
                'class' => []
            ],
            'fieldset' => [],
            'form' => [
                'name' => [],
                'method' => [],
            ],
            'h1' => [],
            'h2' => [],
            'h3' => [],
            'h4' => [],
            'hr' => [],
            'img' => [
                'alt' => [],
                'class' => [],
                'src' => [],
                'width' => [],
            ],
            'input' => [
                'id' => [],
                'class' => [],
                'name' => [],
                'type' => [],
                'readonly' => [],
                'value' => [],
            ],
            'label' => [
                'class' => [],
                'for' => [],
            ],
            'li' => [
                'class' => []
            ],
            'textarea' => [
                'class' => [],
                'id' => [],
                'help' => [],
                'name' => [],
            ],
            'script' => [],
            'small' => [
                'class' => []
            ],
            'strong' => [],
            'style' => [],
            'ul' => [
                'class' => []
            ],
        ];

        echo \wp_kses($this->twig->render('Assets/js/constants.js.twig', $vars), $allowed_html);
    }
}
