<?php

declare(strict_types=1);

use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\CustomerRegistration;
use Magento\Customer\Block\Account\Customer;
use Magento\Customer\Model\Account\Redirect;
use Magento\Framework\Escaper;

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

/** @var \FoundationCommerce\ThemeViewModels\ViewModels\ThemeIcons $icons */
$icons = $viewModels->require(\FoundationCommerce\ThemeViewModels\ViewModels\ThemeIcons::class);

/** @var CustomerRegistration $customerRegistration */
$customerRegistration = $viewModels->require(CustomerRegistration::class);
?>

<div x-data="{ open: false }" class="relative">
    <a
        href="<?= $escaper->escapeHtmlAttr($escaper->escapeUrl($block->getUrl('customer/account'))) ?>"
        id="customer-mobile-menu"
        type="button"
        class="block hover:text-black px-4"
        @click.prevent="open = true"
        aria-haspopup="true"
        aria-expanded="true"
        aria-label="<?= $escaper->escapeHtmlAttr(__('My Account')) ?>"
    >
        <img class="mx-auto"
             src="<?= $escaper->escapeHtmlAttr($block->getViewFileUrl('images/user.png')) ?>"
             alt="User Image"
             width="20"
             height="22"
        >
        <span class="label text-sm leading-4">
        <?= $escaper->escapeHtml(__('Account')) ?>
        </span>
    </a>
    <nav class="absolute right-0 z-20 hidden w-40 py-2 mt-2 -mr-4 px-1 overflow-auto origin-top-right rounded-sm
        shadow-lg sm:w-48 lg:mt-3 bg-container-lighter"
         :class="{ 'hidden' : !open }"
         @click.away="open = false"
         aria-labelledby="customer-mobile-menu"
    >
        <?php if ($block->customerLoggedIn()): ?>
            <a class="block px-4 py-2 lg:px-5 lg:py-2 hover:bg-gray-100"
               href="<?= $escaper->escapeUrl($block->getUrl('customer/account')) ?>"
            >
                <?= $escaper->escapeHtml(__('My Account')); ?>
            </a>
            <a class="block px-4 py-2 lg:px-5 lg:py-2 hover:bg-gray-100"
               href="<?= $escaper->escapeUrl($block->getUrl('wishlist')) ?>"
            >
                <?= $escaper->escapeHtml(__('My Wish List')); ?>
            </a>
            <a class="block px-4 py-2 lg:px-5 lg:py-2 hover:bg-gray-100"
               href="<?= $escaper->escapeUrl($block->getUrl('sales/order/history')) ?>"
            >
                <?= $escaper->escapeHtml(__('My Orders')); ?>
            </a>
            <a class="block px-4 py-2 lg:px-5 lg:py-2 hover:bg-gray-100"
               href="<?= $escaper->escapeUrl($block->getUrl('customer/address')) ?>"
            >
                <?= $escaper->escapeHtml(__('Address Book')); ?></a>
            <a class="block px-4 py-2 lg:px-5 lg:py-2 hover:bg-gray-100"
               href="<?= $escaper->escapeUrl($block->getUrl('customer/account/logout')) ?>"
            >
                <?= $escaper->escapeHtml(__('Sign Out')); ?>
            </a>
        <?php else: ?>
            <a class="block px-4 py-2 lg:px-5 lg:py-2 hover:bg-gray-100"
               onclick="hyva.setCookie && hyva.setCookie(
                   '<?= /** @noEscape */ Redirect::LOGIN_REDIRECT_URL ?>',
                   window.location.href,
                   1
               )"
               href="<?= $escaper->escapeUrl($block->getUrl('customer/account/index')) ?>">
                <?= $escaper->escapeHtml(__('Sign In')); ?>
            </a>
            <?php if ($customerRegistration->isAllowed()): ?>
                <a class="block px-4 py-2 lg:px-5 lg:py-2 hover:bg-gray-100"
                   href="<?= $escaper->escapeUrl($block->getUrl('customer/account/create')) ?>"
                >
                    <?= $escaper->escapeHtml(__('Create an Account')); ?>
                </a>
            <?php endif; ?>
        <?php endif; ?>
    </nav>
</div>
