<?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_QuickCart
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\Wishlist;
use Magento\Framework\Escaper;

/** @var ViewModelRegistry $viewModels */
/** @var Escaper $escaper */
/** @var Wishlist $wishlistViewModel */
$wishlistViewModel = $viewModels->require(Wishlist::class);
?>
<?php if ($wishlistViewModel->isEnabled()): ?>
    <?php
    $ajaxCart       = $block->getLayout()->createBlock('Mageplaza\QuickCart\Block\AjaxCart\Popup');
    $isAjaxWishlist = $this->helper('Mageplaza\QuickCart\Helper\QuickViewData')->isAjaxWishlist();
    ?>

    <script>
        function initWishlist() {
            return {
                addToWishlist(productId) {
                    let optionsData = <?= /** @noEscape */ $ajaxCart->wishListAndCompareData() ?>;
                    let isAjaxWishlist = <?=  /** @noEscape */  json_encode($isAjaxWishlist) ?>;
                    const formKey = hyva.getFormKey();
                    const postUrl = BASE_URL + 'wishlist/index/add/' + '?' +  "form_key=" + formKey + "&product=" + productId + "&uenc=" + hyva.getUenc();
                    fetch(postUrl, {
                        "headers": {
                            "content-type": "application/json",
                            'X-Requested-With': 'XMLHttpRequest'
                        },
                        "method": "POST",
                        "mode": "cors",
                        "credentials": "include"
                    }).then(function (response) {
                        if (response.redirected) {
                            window.location.href = response.url;

                        } else if (response.ok) {
                            if (isAjaxWishlist) {
                                const isQuickView = document.querySelector('.mpquickview-popup #maincontent');
                                if ((isQuickView && !optionsData.isApplyQV) ||
                                    (!isQuickView && !optionsData.isApplyPage)) {
                                    window.location.reload();
                                    return;
                                }
                                return response.json();
                            }
                            window.location.reload();
                        } else {
                            typeof window.dispatchMessages !== "undefined" && window.dispatchMessages(
                                [{
                                    type: "warning",
                                    text: "<?= $escaper->escapeHtml(__('Could not add item to wishlist.')) ?>"
                                }], 5000
                            );
                        }
                    }).then(function (response) {
                        if (!response) { return }
                        const reloadCustomerDataEvent = new CustomEvent("reload-customer-section-data");
                        window.dispatchEvent(reloadCustomerDataEvent);
                    }).catch(function (error) {
                        typeof window.dispatchMessages !== "undefined" && window.dispatchMessages(
                            [{
                                type: "error",
                                text: error
                            }], 5000
                        );
                    });
                }
            }
        }
    </script>
<?php endif; ?>
