{{--
 * Blade Template
 *
 * Usage:       <x-globals::global.language-switch languageContext="{{ \BplanBase\Globals\Enums\LanguageContext::Both }}" />
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Blades
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025, 2026 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
--}}

@php

    $languageContext ??= \BplanBase\Globals\Enums\LanguageContext::Both;

    // $availableLocales = \Registry::get('core.tenantLocales');
    $availableLocales = \BplanBase\Globals\Repositories\LanguageRepository::getByContext($languageContext)
      ->pluck('code')
      ->toArray();

    $currentLocale = \Registry::get('appLocale');

    $localeCount = count($availableLocales);

@endphp

@if ($localeCount >= 2)

    @if ($localeCount === 2) {{-- Toggle mode: a simple click switches to the other language --}}
        @php
            $otherLocale = collect($availableLocales)->first(fn($l) => $l !== $currentLocale);
        @endphp

            <a href="{{ route('locale.set', ['locale' => $otherLocale]) }}"
                class="flex items-center py-2 px-3 text-gray-900 rounded-sm hover:bg-gray-100 md:hover:bg-transparent md:p-0 dark:text-white dark:hover:bg-gray-700 md:dark:hover:bg-transparent"
                title="{{ __('globals::global.language', [], $otherLocale) }}">
              <span class="fi fi-{{ __('globals::global.iso-3166-1-alpha-2', [], $currentLocale) }} rounded text-xl rounded border border-gray-500 dark:border-gray-400" style="line-height:0.95em;"></span>
            </a>

    @else {{-- Dropdown mode: all languages as a list --}}

            <button id="dropdownLocaleButton" data-dropdown-toggle="dropdownLocale" data-dropdown-placement="bottom" type="button"
                class="flex items-center py-2 px-3 text-gray-900 rounded-sm hover:bg-gray-100 md:hover:bg-transparent md:p-0 dark:text-white dark:hover:bg-gray-700 md:dark:hover:bg-transparent">
              <span class="fi fi-{{ __('globals::global.iso-3166-1-alpha-2', [], $currentLocale) }} rounded text-xl rounded border border-gray-500 dark:border-gray-400 leading-[0.95em]" style="line-height:0.95em;"></span>
              <svg class="w-2.5 h-2.5 ms-1.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 10 6">
                <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 4 4 4-4" />
              </svg>
            </button>

            <div id="dropdownLocale" class="z-50 hidden font-normal bg-white divide-y divide-gray-100 rounded-lg shadow-sm w-36 dark:bg-gray-700 dark:divide-gray-600">
              <ul class="py-1 text-sm text-gray-700 dark:text-gray-400" role="none">

        @foreach ($availableLocales as $locale)

                <li>
                  <a href="{{ route('locale.set', ['locale' => $locale]) }}"
                      class="flex items-center gap-2 px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white {{ $locale === $currentLocale ? 'font-semibold text-blue-600 dark:text-blue-400' : '' }}"
                      role="menuitem">
                    <span class="fi fi-{{ __('globals::global.iso-3166-1-alpha-2', [], $locale) }} rounded"></span>
                    <span>{{ __('globals::global.language', [], $locale) }}</span>
                  </a>
                </li>

        @endforeach

              </ul>
            </div>

    @endif
@endif
