<?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
 */

declare(strict_types=1);

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\HeroiconsSolid;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Escaper;

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

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

/** @var string[] $item */
$item = $block->getItem();

/** @var int $level */
$level = $block->getLevel() ?: 0;

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

$menuId = str_replace('-', '_',$item['id']);
$itemClasses = (string) $block->getItemClasses() ?: "flex items-center gap-2 py-1 px-6 border-b";
$itemIconSize = (int) $block->getItemIconSize() ?: 20;
$itemIndent = 'ms-7';
?>

<?php if (!empty($item['childData'])): ?>
    <li data-level="<?= $level ?>" x-data="initMobileSubMenu">
        <button
            type="button"
            class="text-black font-medium w-full justify-between <?= $escaper->escapeHtmlAttr($itemClasses) ?>"
            aria-controls="<?= $escaper->escapeHtml($item['id']) ?>-mobile-menu-panel"
            aria-expanded="false"
            x-ref="openSubMenuBtn"
            :aria-expanded="isOpen"
            :disabled="isOpen"
            @click="open"
        >
            <span class="<?= $level > 0 ? $itemIndent : '' ?>"><?= $escaper->escapeHtml($item['name']) ?></span>
            <span class="bg-transparent text-current">
                <?= $heroiconsSolid->chevronRightHtml('w-8 h-8', $itemIconSize, $itemIconSize, ['aria-hidden' => 'true']) ?>
            </span>
        </button>
        <ul
            id="<?= $escaper->escapeHtml($item['id']) ?>-mobile-menu-panel"
            class="z-10 absolute inset-0 bg-white overflow-y-auto overscroll-y-contain transition duration-300"
            x-show="isOpen"
            x-transition:enter-start="opacity-0 translate-x-8"
            x-transition:enter-end="opacity-100 translate-x-0"
            x-transition:leave-start="opacity-100 translate-x-0"
            x-transition:leave-end="opacity-0 translate-x-8"
            @keydown.escape.stop.prevent="close"
        >
            <li data-level="<?= $level + 1 ?>">
                <button
                    type="button"
                    class="w-full bg-slate-50 font-bold <?= $escaper->escapeHtmlAttr($itemClasses) ?>"
                    @click="close"
                    x-ref="closeSubMenuBtn"
                >
                    <?= $heroiconsSolid->chevronLeftHtml('', $itemIconSize, $itemIconSize, ['aria-hidden' => 'true']) ?>
                    <?= $escaper->escapeHtml($item['name']) ?>
                </button>
                <a
                    href="<?= $escaper->escapeUrl($item['url']) ?>"
                    class="<?= $escaper->escapeHtmlAttr($itemClasses) ?>"
                >
                    <span class="<?= $escaper->escapeHtmlAttr($itemIndent) ?> text-primary-darker font-inter font-semibold">
                        <?= $escaper->escapeHtml(__('See all %1', $item['name'])) ?>
                    </span>
                </a>
            </li>
            <?php foreach ($item['childData'] as $subMenu): ?>
                <?= $block->getLayout()->getBlock('topmenu_mobile_item')
                    ->setItem($subMenu)
                    ->setLevel($level + 1)
                    ->toHtml() ?>
            <?php endforeach; ?>
        </ul>
    </li>
<?php else: ?>
    <li data-level="<?= $level ?>">
        <a
            href="<?= $escaper->escapeUrl($item['url']) ?>"
            class="text-black font-medium <?= $escaper->escapeHtmlAttr($itemClasses) ?>"
        >
            <span class="<?= $level > 0 ? $itemIndent : '' ?>"><?= $escaper->escapeHtml($item['name']) ?></span>
        </a>
    </li>
<?php endif; ?>
