<?php /** @noinspection PhpUndefinedConstantInspection */

use Bitrix\Main\Config\Option;
use intec\Core;
use intec\core\helpers\JavaScript;
use intec\kosmos\resources\{CssVariables, ResourceManager};
use intec\template\Template;

if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) die();

$resources = ResourceManager::getInstance();
$template = Template::getInstance();
$scheme = CssVariables::getInstance();
$settings = $template->settings->getData();
$app = JavaScript::toObject([
    'debug' => false,
    'environment' => Core::$app->browser->getIsMobile() || Core::$app->browser->getIsTablet() ? (Core::$app->browser->getIsMobile() ? 'mobile' : 'tablet') : 'desktop',
    'language' => LANGUAGE_ID,
    'public' => !defined('EDITOR'),
    'site' => [
        'id' => SITE_ID,
        'directory' => SITE_DIR,
        'catalog' => [
            'compare' => Option::get('intec.kosmoscorp', 'compareName')
        ]
    ],
    'template' => [
        'id' => SITE_TEMPLATE_ID,
        'directory' => SITE_TEMPLATE_PATH . '/'
    ],
    'lazyLoad' => [
        'empty' => SITE_TEMPLATE_PATH . '/images/picture.missing.svg',
        'stub' => SITE_TEMPLATE_PATH . '/images/picture.loading.svg'
    ],
    'scripts' => [],
    'styles' => []
]);

foreach ($settings['base'] as $key => $listItem) {
    foreach ($listItem['base']['VARIANTS'][$key]['VARIANTS'] as &$variant) {
        $variant['TYPE'] = 'TEXT';

        if ($variant['CODE'] === $listItem) {
            $variant['ACTIVE'] = true;
        }

        unset($variant);
    }

    unset($key, $listItem);
}

$baseFont = $template->settings->getDataFrom('base', 'fonts', true);
$secondFont = $template->settings->getDataFrom('base', 'fontsSecond', true);
$buttonRound = match ((string)$settings['base']['buttonRound']['template']) {
    'halfRound' => '8px',
    'round' => '40px',
    default => '2px',
};
$tabsRound = match ((string)$settings['base']['tabsButtonRound']['template']) {
    'halfRound' => '8px',
    'round' => '40px',
    default => '2px',
};

if (!empty($settings['base'])) {
    $scheme->set('scheme', (string)$settings['base']['color']['template'])
        ->set('content-max-width', (string)$settings['base']['width']['template'])
        ->set('ui-button-scheme', (string)$settings['base']['color']['template'])
        ->set('ui-border-radius', (string)$settings['base']['borderRadius']['template'])
        ->set('font-family', (string)$baseFont)
        ->set('font-size-base', (string)$settings['base']['fontSize']['template'])
        ->set('font-size-h1', (string)$settings['base']['titleSize']['template'])
        ->set('button-round', $buttonRound)
        ->set('tabs-button-round', $tabsRound);

    if ($settings['base']['colorBg']['show'])
        $scheme->set('template-background', (string)$settings['base']['colorBg']['template']);

    if ($secondFont !== null)
        $scheme->set('font-family-second', (string)($secondFont !== $baseFont ? $secondFont : $baseFont));
    else
        $scheme->set('font-family-second', (string)$baseFont);
}

unset($template, $settings, $baseFont, $secondFont);

$resources->addJsExt(SITE_TEMPLATE_PATH . '/assets/js/application.js', true, false, false, true);
$resources->addJsExt(SITE_TEMPLATE_PATH . '/assets/js/custom.js', true, false, false, true);
$resources->addJsString("application=application($app);document.addEventListener('DOMContentLoaded',()=>{application.options.site.csrf=BX.bitrix_sessid();application.executeStoredScripts()});");
$resources->addJsRemote('https://cdn.jsdelivr.net/npm/intl-tel-input@21.2.3/build/js/intlTelInput.min.js', false, false, false, true);
$resources->addCssRemote('https://cdn.jsdelivr.net/npm/intl-tel-input@21.2.3/build/css/intlTelInput.css');
$resources->addCssString($scheme->compileScss(SITE_TEMPLATE_PATH . '/assets/css/scheme.scss'));
$resources->addCssExt(SITE_TEMPLATE_PATH . '/assets/css/application.css', true);
$resources->addCssExt(SITE_TEMPLATE_PATH . '/assets/css/additional_style.css');

unset($app, $resources, $scheme);
