<?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\HeroiconsOutline;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;

/** @var Template $block */
/** @var Escaper $escaper */
/** @var ViewModelRegistry $viewModels */
/** @var HeroiconsOutline $heroicons */
$heroicons = $viewModels->require(HeroiconsOutline::class);
?>

<div class="block <?= $escaper->escapeHtmlAttr($block->getBlockCss()) ?> card filter-option rounded border-2 md:border-0 py-4 px-8 md:px-3 lg:px-8"
     x-data="initAccountNavigation()"
     x-init="checkIsMobileResolution()"
     @resize.window.debounce="checkIsMobileResolution()"
     @visibilitychange.window.debounce="checkIsMobileResolution()"

>
    <button
        type="button"
        class="
            title <?= $escaper->escapeHtmlAttr($block->getBlockCss()) ?>-title
            flex justify-between
            items-center hover:text-secondary-darker w-full
        "
        @click="blockOpen = !blockOpen"
        :aria-expanded="blockOpen"
        :aria-disabled="!isMobile"
        :disabled="!isMobile ? '' : null"
    >

        <span class="text-xl font-sen font-semibold title">
            <?= $escaper->escapeHtml(__($block->getData('block_title'))) ?>
        </span>
        <span
            class="px-1 py-1 md:hidden"
            x-ref="AccountNavigationMobileToggleIcon"
        >
            <?= $heroicons->chevronDownHtml(
                'transition-transform duration-300 ease-in-out transform rotate-180',
                24,
                24,
                [
                    ':class'      => "{ 'rotate-180': blockOpen }",
                    'aria-hidden' => 'true',
                    'focusable' => 'false'
                ]
            ); ?>
        </span>
    </button>
    <div :class="{ 'hidden': !blockOpen }" class="delimiter border-b border-container w-full mt-4 mb-3 hidden md:block"></div>
    <div class="content <?= $escaper->escapeHtmlAttr($block->getBlockCss()) ?>-content hidden md:block"
         :class="{ 'hidden': !blockOpen }"
         id="<?= $escaper->escapeHtmlAttr($block->getBlockCss()) ?>"
    >
        <?= $block->getChildHtml() ?>
    </div>
</div>
<script>
    function initAccountNavigation() {
        return {
            isMobile: false,
            blockOpen: false,
            checkIsMobileResolution() {
                const mobileElement = this.$refs.AccountNavigationMobileToggleIcon;
                this.isMobile = mobileElement
                    ? getComputedStyle(mobileElement).display !== 'none'
                    : window.matchMedia('(max-width: 767px)').matches; // Fallback to `md` breakpoint
            }
        }
    }
</script>
