<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes. All rights reserved.
 * See COPYING.txt for license details.
 */

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="actions-toolbar my-4">
            <div class="flex flex-wrap gap-2">
                <?= $block->getChildHtml('control_buttons') ?>
            </div>
            <div class="secondary">
                <a href="<?= $escaper->escapeUrl($block->getBackUrl()) ?>" class="action back">
                    <span><?= $escaper->escapeHtml(__('Back')) ?></span>
                </a>
            </div>
        </div>
    </form>
    <div class="toolbar wishlist-toolbar">
        <?= $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 ?>
