<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Smtp
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

use Mageplaza\Smtp\Block\Script;
use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\HyvaCsp;

/** @var Script $block */
$shopId        = $block->getHelperEmailMarketing()->getAppID();
$isSuccessPage = $block->isSuccessPage();
$customerData  = $block->getCustomerData();

/** @var ViewModelRegistry $viewModels */
/** @var HyvaCsp $hyvaCsp */
$isHaveCsp = class_exists(HyvaCsp::class);
if ($isHaveCsp) {
    $hyvaCsp = $viewModels->require(HyvaCsp::class);
}
?>
<script>
    window.AVADA_EM                   = window.AVADA_EM || {};
    window.AVADA_EM_PUSH_ENABLED      = Boolean(<?= /* @noEscape */ $block->getHelperEmailMarketing()->isPushNotification() ?>);
    window.AVADA_EM.shopId            = "<?= /* @noEscape */ $shopId ?>";
    window.AVADA_EM.isTrackingEnabled = Boolean(<?= /* @noEscape */ $block->getHelperEmailMarketing()->isTracking() ?>);
    window.AVADA_EM.template          = "<?= /* @noEscape */ "" ?>";
    window.AVADA_EM.currency          = "<?= /* @noEscape */ $block->getCurrencyCode() ?>";
    window.AVADA_EM.customer          = {
        email: "<?=  /* @noEscape */ $customerData['email'] ?>",
        firstName: "<?=  /* @noEscape */ $customerData['firstname'] ?>",
        lastName: "<?=  /* @noEscape */ $customerData['lastname'] ?>"
    };
</script>
<?php if ($isHaveCsp) :?>
    <?php $hyvaCsp->registerInlineScript();?>
<?php endif; ?>
<!-- BEGIN AVADA EMAIL MARKETING SCRIPT-->
<?php if ($isSuccessPage && $block->getCurrentOrder()) {
    $order        = $block->getCurrentOrder();
    $grandTotal   = $order->getGrandTotal();
    $currencyCode = $order->getOrderCurrencyCode();
    $checkoutId   = $order->getQuoteId();
    $email        = $order->getCustomerEmail();

    ?>
    <script data-cfasync="false" type="text/javascript">
        window.AVADA_EM.vendor   = "magento";
        window.AVADA_EM.checkout = {
            revenue: "<?=  /* @noEscape */ $grandTotal ?>",
            currency: "<?=  /* @noEscape */ $currencyCode ?>",
            checkoutId: "<?=  /* @noEscape */ $checkoutId ?>",
            checkoutEmail: "<?=  /* @noEscape */ $email ?>"
        };
    </script>
    <?php if ($isHaveCsp) :?>
        <?php $hyvaCsp->registerInlineScript();?>
    <?php endif; ?>
<?php } ?>

<?php if ($product = $block->productAbandoned()) : ?>
    <script data-cfasync="false" type="text/javascript">
        window.AVADA_EM.product  = {
            collections: [],
            id: "<?= /* @noEscape */ $product['id'] ?>",
            image: "<?= /* @noEscape */ $product['image'] ?>",
            price: "<?= /* @noEscape */ $product['price'] ?>",
            priceTax: "<?= /* @noEscape */ $product['priceTax'] ?>",
            productType: "<?= /* @noEscape */ $product['productType'] ?>",
            tags: [],
            title: "<?= /* @noEscape */ $product['title'] ?>",
            url: "<?= /* @noEscape */ $product['url'] ?>",
            vendor: "magento"
        };
        window.AVADA_EM.template = "product";
    </script>
    <?php if ($isHaveCsp) :?>
        <?php $hyvaCsp->registerInlineScript();?>
    <?php endif; ?>
<?php endif; ?>
<script data-cfasync="false" type="text/javascript">(function(b){var s=document.createElement("script");s.type="text/javascript";s.async=true;s.src=b;var x=document.getElementsByTagName("script")[0];x.parentNode.insertBefore(s,x);})("https://app.avada.io/avada-sdk.min.js");</script>
<?php if ($isHaveCsp) :?>
    <?php $hyvaCsp->registerInlineScript();?>
<?php endif; ?>
