<?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\Currency;
use Hyva\Theme\ViewModel\HeroiconsSolid;
use Hyva\Theme\ViewModel\HyvaCsp;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

// phpcs:disable Generic.Files.LineLength.TooLong

/** @var Template $block */
/** @var Escaper $escaper */
/** @var HyvaCsp $hyvaCsp   */
/** @var ViewModelRegistry $viewModels */

/** @var HeroiconsSolid $heroiconsSolid */
$heroiconsSolid = $viewModels->require(HeroiconsSolid::class);

/** @var Currency $currencyViewModel */
$currencyViewModel = $viewModels->require(Currency::class);
?>
<?php if ($currencyViewModel->getCurrencyCount() > 1): ?>
    <?php $currencies = $currencyViewModel->getCurrencies(); ?>
    <?php $currentCurrencyCode = $currencyViewModel->getCurrentCurrencyCode(); ?>
    <div x-data="initCurrencySwitcher"
         class="w-full sm:w-1/2 md:w-full pr-4"
    >
        <h2
            id="currency-heading"
            class="text-md leading-5 font-semibold tracking-wider uppercase mb-4"
        >
            <?= $escaper->escapeHtml(__('Currency')) ?>
        </h2>
        <div class="relative inline-block text-left">
            <div>
                <button @click.prevent="toggleOpen"
                        @click.outside="setOpenFalse"
                        @keydown.window.escape="setOpenFalse"
                        type="button"
                        class="form-select w-full pl-4"
                        aria-haspopup="true"
                        :aria-expanded="ariaExpanded"
                >
                    <?= $escaper->escapeHtml($currentCurrencyCode) ?>
                    <?php if ($currencies[$currentCurrencyCode]): ?>
                        - <?= $escaper->escapeHtml($currencies[$currentCurrencyCode]) ?>
                    <?php endif; ?>
                </button>
            </div>
            <nav x-cloak
                 x-show="open"
                 class="absolute right-0 top-full z-20 w-full lg:w-56 py-2 mt-1 overflow-auto origin-top-left rounded-sm shadow-lg sm:w-48 lg:mt-3 bg-white"
                 aria-labelledby="currency-heading"
            >
                <div class="my-1">
                    <?php foreach ($currencies as $code => $name): ?>
                        <?php if ($code != $currentCurrencyCode): ?>
                            <button
                               role="link"
                               class="w-full text-start px-4 py-2 lg:px-5 lg:py-2 hover:bg-gray-100"
                               aria-describedby="currency-heading"
                               @click.prevent="switchCurrency"
                               data-currency-data="<?= $escaper->escapeHtmlAttr($currencyViewModel->getSwitchCurrencyPostData($code)) ?>"
                            >
                                <?= $escaper->escapeHtml($code) ?> - <?= $escaper->escapeHtml($name) ?>
                            </button>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </div>
            </nav>
        </div>
    </div>
    <script>
        function initCurrencySwitcher() {
            return hyva.createBooleanObject('open', false, {
                ariaExpanded() {
                    return this.open() ? 'true' : 'false';
                },
                switchCurrency() {
                    const data = JSON.parse(this.$el.dataset.currencyData);
                    hyva.postForm(data);
                }
            });
        }
        window.addEventListener('alpine:init', () => Alpine.data('initCurrencySwitcher', initCurrencySwitcher), {once: true})
    </script>
    <?php $hyvaCsp->registerInlineScript() ?>
<?php endif; ?>
