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

declare(strict_types=1);

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\CurrentProduct;
use Hyva\Theme\ViewModel\ProductPage;
use Hyva\Theme\ViewModel\ProductPrice;
use Magento\Catalog\Block\Product\View;
use Magento\Catalog\Model\Product;
use Magento\Framework\Escaper;

/** @var View $block */
/** @var Escaper $escaper */
/** @var Product $product */
/** @var ViewModelRegistry $viewModels */

/** @var ProductPrice $productPriceViewModel */
$productPriceViewModel = $viewModels->require(ProductPrice::class);

/** @var ViewModelRegistry $viewModels */
$currentProduct = $viewModels->require(CurrentProduct::class);

$product = $currentProduct->get();

if (!$product) {
    return '';
}

/** @var ProductPage $productViewModel */
$productViewModel = $viewModels->require(ProductPage::class);

?>
<script>
    function initTierPrices<?= (int) $product->getId() ?>() {
        return {
            activeProductsPriceData: false,
            getConfiguredTierPrices() {
                if (this.activeProductsPriceData && this.activeProductsPriceData.tierPrices) {
                    return this.activeProductsPriceData.tierPrices.map((tierPrice) => {
                        return {
                            qty: tierPrice.qty,
                            price: tierPrice.price,
                            basePrice: tierPrice.basePrice,
                            percentage: tierPrice.percentage
                        }
                    })
                }
                return false;
            },
            getItemHtml(item) {
                const price = <?php if ($productPriceViewModel->displayPriceInclAndExclTax()): ?>
                    `${hyva.formatPrice(item.price)} (<?= $escaper->escapeJs(__('Excl. Tax')) ?>: ${hyva.formatPrice(item.basePrice)})`;
                <?php else: ?>
                    hyva.formatPrice(item.price);
                <?php endif; ?>
                return '<?= $escaper->escapeHtml(__('Buy %1 for %2 each and', '%1', '%2')) ?>'
                    .replace('%1', item.qty)
                    .replace('%2', price)

            },
            eventListeners: {
                ['@update-prices-<?= (int) $product->getId() ?>.window'](event) {
                    this.activeProductsPriceData = event.detail;
                }
            }
        }
    }
</script>
<div
     x-data="initTierPrices<?= (int)$product->getId() ?>()"
     x-bind="eventListeners"
>
    <template x-if="getConfiguredTierPrices()">
        <ul role="list" class="prices-tier items mb-4">
            <template x-for="tierPrice in getConfiguredTierPrices()">
                <li class="item bg-white p-2 shadow mb-2 border-l-4 border-green-400 mx-1">
                    <span x-html="getItemHtml(tierPrice)"></span>
                    <strong class="benefit">
                        <?= $escaper->escapeHtml(__('save')) ?>
                        <span class="percent" x-text="tierPrice.percentage + '%'"></span>
                    </strong>
                </li>
            </template>
        </ul>
    </template>
</div>
