@php
    use Filament\Panel;

    /** @var array<string, string> $locales */
    $locales  = config('dberp.admin.locales', []);
    $current  = app()->getLocale();
    $useDropdown = count($locales) > 2;

    /** 顶栏等场景下 currentPanel 偶发未挂载，回退到 admin 面板（与本 PanelProvider 的 id 一致） */
    $panel = filament()->getCurrentPanel() ?? filament()->getPanel('admin');

    $dberpLocaleSwitchUrl = function (string $code) use ($panel): string {
        if ($panel instanceof Panel) {
            return $panel->route('locale.switch', ['locale' => $code]);
        }

        return route('filament.admin.locale.switch', ['locale' => $code]);
    };

    $dberpLocaleSwitchOuterClass ??= 'fi-admin-locale-switch ms-2 me-1 flex items-center';

    // SVG 地球图标（复用）
    $globeSvg = '<svg class="h-3.5 w-3.5 shrink-0 opacity-75" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">'
        . '<path d="M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1ZM3.2 5.6h1.4c.1-.7.3-1.3.6-1.8A5.3 5.3 0 0 0 3.2 5.6Zm-.5 1.4A5.3 5.3 0 0 0 2.7 8c0 .35.03.7.1 1H4.2A9 9 0 0 1 4.1 8c0-.34.03-.68.1-1H2.7Zm.7 3.4A5.3 5.3 0 0 0 5.2 12.2a6.1 6.1 0 0 1-.6-1.8H3.4Zm4.6 2a5 5 0 0 1-1.6-2h1.6v2Zm0-3H5.6A7.6 7.6 0 0 1 5.4 8c0-.35.07-.7.2-1H8v2Zm0-3H6a5 5 0 0 1 2-2v2Zm.7 6V13.4A5 5 0 0 0 10.6 11.4H8.7Zm0-7V2.4a5 5 0 0 1 1.6 2H8.7Zm0 4V7h2.4c.13.3.2.65.2 1s-.07.7-.2 1H8.7Zm0-5V3.8a5.3 5.3 0 0 1 1.6 1.8H8.7Zm3.1 0a5.3 5.3 0 0 0-1.9-1.8c.27.5.48 1.1.6 1.8h1.3Zm.5 1.4h-1.5c.07.32.1.66.1 1s-.03.68-.1 1h1.5c.07-.3.1-.65.1-1s-.03-.7-.1-1Zm-.5 3.4h-1.3a6 6 0 0 1-.6 1.8 5.3 5.3 0 0 0 1.9-1.8Z"/>'
        . '</svg>';
@endphp

@if (! empty($locales))
<div
    class="{{ $dberpLocaleSwitchOuterClass }}"
    role="group"
    aria-label="{{ __('dberp.admin.locale.group_label') }}"
>

    @if ($useDropdown)
        {{-- ≥ 3 种语言：下拉菜单 --}}
        <div x-data="{ open: false }" class="relative">
            <button
                type="button"
                @click="open = !open"
                @click.outside="open = false"
                class="inline-flex items-center gap-1.5 rounded-lg border border-gray-200 bg-gray-50 px-2.5 py-1 text-xs font-medium text-gray-600 transition hover:bg-white hover:shadow-sm dark:border-white/10 dark:bg-white/5 dark:text-gray-400 dark:hover:bg-white/10"
            >
                {!! $globeSvg !!}
                <span>{{ $locales[$current] ?? $current }}</span>
                <svg class="h-3 w-3 opacity-60 transition-transform duration-150" :class="{ 'rotate-180': open }" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
                    <path fill-rule="evenodd" d="M4.22 6.22a.75.75 0 0 1 1.06 0L8 8.94l2.72-2.72a.75.75 0 1 1 1.06 1.06l-3.25 3.25a.75.75 0 0 1-1.06 0L4.22 7.28a.75.75 0 0 1 0-1.06Z" clip-rule="evenodd"/>
                </svg>
            </button>

            <div
                x-show="open"
                x-transition:enter="transition ease-out duration-100"
                x-transition:enter-start="opacity-0 scale-95"
                x-transition:enter-end="opacity-100 scale-100"
                x-transition:leave="transition ease-in duration-75"
                x-transition:leave-start="opacity-100 scale-100"
                x-transition:leave-end="opacity-0 scale-95"
                class="absolute end-0 z-50 mt-1 min-w-[7rem] rounded-lg border border-gray-200 bg-white py-1 shadow-lg dark:border-white/10 dark:bg-gray-800"
                style="display: none;"
            >
                @foreach ($locales as $code => $label)
                    <a
                        href="{{ $dberpLocaleSwitchUrl($code) }}"
                        @class([
                            'flex items-center gap-2 px-3 py-1.5 text-xs transition',
                            'font-semibold text-primary-600 dark:text-primary-400' => $current === $code,
                            'text-gray-700 hover:bg-gray-50 dark:text-gray-300 dark:hover:bg-white/5' => $current !== $code,
                        ])
                    >
                        @if ($current === $code)
                            <svg class="h-3 w-3 shrink-0 text-primary-500" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
                                <path fill-rule="evenodd" d="M12.78 4.22a.75.75 0 0 1 0 1.06l-6.5 6.5a.75.75 0 0 1-1.06 0l-3-3a.75.75 0 0 1 1.06-1.06l2.47 2.47 5.97-5.97a.75.75 0 0 1 1.06 0Z" clip-rule="evenodd"/>
                            </svg>
                        @else
                            <span class="h-3 w-3 shrink-0"></span>
                        @endif
                        {{ $label }}
                    </a>
                @endforeach
            </div>
        </div>

    @else
        {{-- ≤ 2 种语言：胶囊按钮组 --}}
        <div class="flex items-center rounded-xl border border-gray-200 bg-gray-50 p-0.5 dark:border-white/10 dark:bg-white/5">
            @foreach ($locales as $code => $label)
                <a
                    href="{{ $dberpLocaleSwitchUrl($code) }}"
                    title="{{ $label }}"
                    @class([
                        'inline-flex items-center gap-1.5 rounded-lg px-3 py-1 text-xs font-medium leading-none transition-all',
                        'bg-primary-600 text-white shadow-sm dark:bg-primary-500' => $current === $code,
                        'text-gray-500 hover:bg-white hover:text-gray-800 hover:shadow-sm dark:text-gray-400 dark:hover:bg-white/10 dark:hover:text-gray-200' => $current !== $code,
                    ])
                >
                    {!! $globeSvg !!}
                    {{ $label }}
                </a>
            @endforeach
        </div>

    @endif

</div>
@endif
