<?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 FoundationCommerce\ThemeViewModels\ViewModels\StoreInfoProvider;
use Hyva\Theme\Model\ViewModelRegistry;
use Hyva\Theme\ViewModel\HeroiconsSolid;
use Hyva\Theme\ViewModel\Store;
use Hyva\Theme\ViewModel\StoreSwitcher;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;
use Magento\Store\ViewModel\SwitcherUrlProvider;

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

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

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

/** @var SwitcherUrlProvider $switcherUrlProvider */
$switcherUrlProvider = $viewModels->require(SwitcherUrlProvider::class);

/** @var Store $storeViewModel */
$storeViewModel = $viewModels->require(Store::class);

/** @var StoreSwitcher $storeSwitcherViewModel */
$storeSwitcherViewModel = $viewModels->require(StoreSwitcher::class);

/** @var StoreInfoProvider $storeInfoViewModel */
$storeInfoViewModel = $viewModels->require(StoreInfoProvider::class);

$currentStore = $storeSwitcherViewModel->getStore();
?>
<?php if (count($storeSwitcherViewModel->getStores()) > 1): ?>
    <div x-data="{ open: false }"
         class="border-r border-gray-700 pr-4"
    >
        <div class="relative inline-block text-left">
            <div>
                <button @click.prevent="open = !open"
                        @click.away="open = false"
                        @keydown.window.escape="open=false"
                        type="button"
                        class="w-full focus:outline-none text-center"
                        aria-haspopup="true"
                        aria-expanded="true"
                >
                    <img src="<?= $block->getViewFileUrl('images/language/' . $storeInfoViewModel->getStoreLocale() . '.svg') ?>"
                         class="w-8 h-8 md:h-6 md:w-6 mx-auto"
                         alt="" />

                    <span class="">
                        <?= $escaper->escapeHtml(__('Language')) ?>
                    </span>
                </button>
            </div>
            <nav x-cloak=""
                 x-show="open"
                 class="absolute left-0 top-full z-20 w-56 py-2 mt-1 overflow-auto origin-top-left rounded-sm shadow-lg sm:w-48 lg:mt-3 bg-container-lighter">
                <div class="my-1" role="menu" aria-orientation="vertical" aria-labelledby="options-menu">
                <?php foreach ($storeSwitcherViewModel->getStores() as $lang): ?>
                    <?php if ($lang->getId() != $storeViewModel->getStoreId()): ?>
                        <a href="<?= $escaper->escapeUrl($switcherUrlProvider->getTargetStoreRedirectUrl($lang)) ?>"
                           class="block px-4 py-2 lg:px-5 lg:py-2 hover:bg-gray-100"
                        >
                            <?= $escaper->escapeHtml($lang->getName()) ?>
                        </a>
                    <?php endif; ?>
                <?php endforeach; ?>
                </div>
            </nav>
        </div>
    </div>
<?php endif; ?>
