<?php

declare(strict_types=1);

use Basecom\SpeculationRulesToolbox\Model\Config;
use Basecom\SpeculationRulesToolbox\ViewModel\SpeculationRules;
use Hyva\Theme\Model\ViewModelRegistry;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

/** @var Template $block */
/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */
/** @var SpeculationRules $speculationRules */

$speculationRules = $viewModels->require(SpeculationRules::class);

// Only render on PDP
if (!$speculationRules->isProductDetailPage()) {
    return;
}

/** @var Config $speculationRules */
$customLinksEnabled = $speculationRules->isPdpCustomLinksEnabled();
$dynamicTargetsEnabled = $speculationRules->isPdpDynamicTargetsEnabled();

if (!$customLinksEnabled && !$dynamicTargetsEnabled) {
    // Early return if no relevant settings are enabled
    return;
}

$preloadTypeCustom = $speculationRules->getPdpPreloadTypeCustom();
$customLinksArray = $speculationRules->getPdpCustomLinks();
$preloadTypeDynamic = $speculationRules->getPdpPreloadTypeDynamic();
$cssSelector = $speculationRules->getPdpCssSelector();
$concurrentPreloads = $speculationRules->getPdpConcurrentPreloads();
?>

<?= /** @noEscape */ $block->getChildBlock('speculationrules.pdp.script')
    ->setData('customLinksEnabled', $customLinksEnabled)
    ->setData('dynamicTargetsEnabled', $dynamicTargetsEnabled)
    ->setData('preloadTypeCustom', $preloadTypeCustom)
    ->setData('customLinksArray', $customLinksArray)
    ->setData('preloadTypeDynamic', $preloadTypeDynamic)
    ->setData('cssSelector', $cssSelector)
    ->setData('concurrentPreloads', $concurrentPreloads)
    ->toHtml();
?>
