<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

use Magento\Framework\Escaper;
use Magento\Wishlist\Block\Customer\Wishlist;
use Magento\Wishlist\Helper\Data as WishlistHelper;

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundHelper

/** @var Escaper $escaper */
/** @var Wishlist $block */

$wishlistHelper = $this->helper(WishlistHelper::class);
?>

<?php if ($wishlistHelper->isAllow()): ?>
    <div class="toolbar wishlist-toolbar"><?= $block->getChildHtml('wishlist_item_pager'); ?></div>
    <?= ($block->getChildHtml('wishlist.rss.link')) ?>
    <form class="form-wishlist-items mt-4"
          id="wishlist-view-form"
          x-data="initMyWishList()"
          action="
          <?= $escaper->escapeUrl(
              $block->getUrl(
                  'wishlist/index/update',
                  ['wishlist_id' => $block->getWishlistInstance()->getId()]
              )
          ) ?>"
          method="post">
        <?= $block->getChildHtml('top') ?>
        <?php if ($block->hasWishlistItems()): ?>
            <?= $block->getBlockHtml('formkey') ?>
            <?php $block->getChildBlock('items')->setItems($block->getWishlistItems()); ?>
            <?= $block->getChildHtml('items') ?>
        <?php else: ?>
            <div class="message info empty">
                <span>
                    <?= $escaper->escapeHtml(__('You have no items in your wish list.')) ?>
                </span>
            </div>
        <?php endif ?>
        <?= $block->getChildHtml('bottom') ?>
        <div class="flex md:flex-row  items-center justify-between pt-4 mb-4 border-none">
            <div class="secondary lg:mr-10">
                <a href="<?= $escaper->escapeUrl($block->getBackUrl()) ?>" class="action back text-primary-darker cursor-pointer text-xs md:text-sm lg:text-base font-sen font-medium">
                    <span><?= $escaper->escapeHtml(__('Back')) ?></span>
                </a>
            </div>
            <div class="flex flex-wrap gap-1 md:gap-2">
                <?= $block->getChildHtml('control_buttons') ?>
            </div>

        </div>
    </form>
    <div class="toolbar wishlist-toolbar hidden">
        <?= $block->getChildHtml('wishlist_item_pager') ?>
    </div>

    <script>
        function initMyWishList() {
            return {
                addToCart(productId, postParams) {

                    const qty = this.$refs['product-qty-'+productId] && this.$refs['product-qty-'+productId].value;

                    postParams.data.qty = qty || postParams.data.qty;

                    hyva.postForm(postParams);

                },
                addAllItemsToCart() {
                    const urlParams = <?= /* @noEscape */ $block->getAddAllToCartParams() ?>;
                    let separator = urlParams.action.indexOf('?') >= 0 ? '&' : '?';

                    Array.from(document.querySelectorAll('input[name^=qty]')).map(qty => {
                        urlParams.action += separator + qty.name + '=' + encodeURIComponent(qty.value);
                        separator = '&';
                    });
                    hyva.postForm(urlParams);
                }
            }
        }
    </script>
<?php endif ?>
