<?php
/**
 * @category Mageants SimpleConfigurable
 * @package Mageants_SimpleConfigurable
 * @copyright Copyright (c) 2017 Mageants
 * @author Mageants Team <info@mageants.com>
 */
?>
<?php /** @var $block \Magento\Swatches\Block\Product\Renderer\Configurable */ ?>
<div class="swatch-opt" data-role="swatch-options"></div>
<?php $isEnable = $block->isEnable(); ?>
<?php if ($isEnable) { ?>
    <?php
        $preselectOptionId = $block->getPreselectOptionId();
        $currentCleanUrl = $block->getCleanUrl();
        $replaceUrl = $block->replaceUrl();
        $mainProductUrl = $this->getProductFullUrlByProduct($block->getProduct());
        $preselctChildProductUrl = '';
        if ($preselectOptionId && $replaceUrl) {
            $preselctChildProductUrl = $block->getMatchingChildProduct($block->getProduct(), $preselectOptionId, true);
        }
    ?>
    <script type="text/x-magento-init">
        {
            "[data-role=swatch-options]": {
                "Magento_Swatches/js/swatch-renderer": {
                    "jsonConfig": <?= /* @noEscape */ $swatchOptions = $block->getJsonConfig() ?>,
                    "jsonSwatchConfig": <?= /* @noEscape */
                        __($swatchOptions = $block->getJsonSwatchConfig()); ?>,
                    "mediaCallback": "<?= /* @noEscape */ $block->getMediaCallback() ?>",
                    "gallerySwitchStrategy": "<?= /* @noEscape */  __($block->getVar(
                        'gallery_switch_strategy',
                        'Magento_ConfigurableProduct'
                    ) ?: 'replace'); ?>",
                    <?php if ($preselectOptionId) {?>
                    "preSelectedOption": <?= /* @noEscape */ __($preselectOptionId);?>,
                    <?php } ?>
                    "mainProductUrl" : "<?= $mainProductUrl ?>",
                    <?php if ($preselctChildProductUrl) {?>
                    "preselctChildProductUrl": <?= /* @noEscape */ __($preselctChildProductUrl);?>,
                    <?php } ?>
                    "replaceUrl" : <?= /* @noEscape */ $replaceUrl; ?>,
                    "is_PDP_page" : true
                }
            }
        }
    </script>
<?php } else { ?>
    <script type="text/x-magento-init">
        {
            "[data-role=swatch-options]": {
                "Magento_Swatches/js/swatch-renderer": {
                    "jsonConfig": <?= /* @noEscape */ $swatchOptions = $block->getJsonConfig() ?>,
                    "jsonSwatchConfig": <?= /* @noEscape */
                         __($swatchOptions = $block->getJsonSwatchConfig()); ?>,
                    "mediaCallback": "<?= /* @noEscape */ $block->getMediaCallback() ?>",
                    "gallerySwitchStrategy": "<?= /* @noEscape */  __($block->getVar(
                        'gallery_switch_strategy',
                        'Magento_ConfigurableProduct'
                    ) ?: 'replace'); ?>"
                }
            }
        }
    </script>
<?php } ?>