<?php

/**
 * Hyva-compatible override of Meta_Conversion::pixel/initiate_checkout.phtml
 *
 * This event only needs to fire once when the checkout page loads, so no
 * jQuery/Knockout hook is actually required - it works as-is once
 * text/x-magento-init is replaced with a direct call.
 *
 * Note: this block only renders if your checkout page layout still
 * includes the standard "before.body.end" container. If you use Hyva
 * Checkout (a separate, self-contained checkout implementation) and it
 * does not render that container, this event will not fire - add the
 * container back via layout XML for your checkout page, or dispatch this
 * block from Hyva Checkout's own layout handle instead.
 *
 * @var \Meta\Conversion\Block\Pixel\InitiateCheckout $block
 * @var \Magento\Framework\Escaper $escaper
 */

if (!function_exists('metaConversionJsonEncode')) {
    /**
     * metaConversionJsonEncode() hardened for direct interpolation into an inline <script> tag.
     */
    function metaConversionJsonEncode($value)
    {
        return json_encode($value, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);
    }
}


$trackerUrl = $block->getTrackerUrl();

if ($block->getFacebookPixelID()):
    ?>
    <script>
        (function () {
            var config = {
                url: <?= /* @noEscape */ metaConversionJsonEncode($trackerUrl) ?>,
                payload: {
                    eventName: <?= /* @noEscape */ metaConversionJsonEncode($block->getEventToObserveName()) ?>,
                    eventId: <?= /* @noEscape */ metaConversionJsonEncode($block->getEventId()) ?>,
                    quoteId: <?= /* @noEscape */ metaConversionJsonEncode($block->getQuote()->getId()) ?>
                },
                browserEventData: {
                    fbAgentVersion: <?= /* @noEscape */ metaConversionJsonEncode($block->getFacebookAgentVersion()) ?>,
                    fbPixelId: <?= /* @noEscape */ metaConversionJsonEncode($block->getFacebookPixelID()) ?>,
                    source: <?= /* @noEscape */ metaConversionJsonEncode($block->getSource()) ?>,
                    pluginVersion: <?= /* @noEscape */ metaConversionJsonEncode($block->getPluginVersion()) ?>,
                    track: 'trackCustom',
                    event: 'InitiateCheckout',
                    payload: {
                        content_type: <?= /* @noEscape */ metaConversionJsonEncode($block->getContentTypeQuote()) ?>,
                        content_ids: <?= /* @noEscape */ metaConversionJsonEncode($block->getContentIDs()) ?>,
                        num_items: <?= /* @noEscape */ metaConversionJsonEncode($block->getNumItems()) ?>,
                        contents: <?= /* @noEscape */ metaConversionJsonEncode($block->getContents()) ?>,
                        content_category: <?= /* @noEscape */ metaConversionJsonEncode($block->getContentCategory()) ?>,
                        value: <?= /* @noEscape */ metaConversionJsonEncode($block->getValue() ?? '') ?>,
                        currency: <?= /* @noEscape */ metaConversionJsonEncode($block->getCurrency() ?? '') ?>
                    }
                }
            };

            function fire() {
                window.MetaConversion.trackEvent(config);
            }

            window.MetaConversion ? fire() : document.addEventListener('DOMContentLoaded', fire);
        })();
    </script>
<?php endif; ?>
