<!-- resources/views/livewire/country-select.blade.php -->
<div 
    x-data="{ open: @entangle('isOpen') }" 
    x-on:click.away="open = false"
    class="relative"
>
    <!-- Botón que activa el desplegable -->
    <button 
        x-on:click="open = !open; $nextTick(() => { $refs.searchInput.focus() })"
        type="button"
        class="flex items-center justify-between w-[300px] border-b-1 border-gray-300 focus:border-blue-500 focus:outline-none"
    >
        <span>
            @if($selectedCountry && array_key_exists($selectedCountry, config('countries')))
                {{ $this->formattedCountry($selectedCountry, config("countries.$selectedCountry")) }}
            @else
                Seleccione un país
            @endif
        </span>
        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
        </svg>
    </button>
    
    <!-- Desplegable -->
    <div 
        x-show="open"
        x-transition
        class="absolute z-10 w-full mt-1 bg-white border rounded-md shadow-lg"
    >
        <!-- Input de búsqueda dentro del desplegable -->
        <div class="p-2 border-b">
            <input
                type="text"
                wire:model.debounce.300ms="search"
                placeholder="Buscar país..."
                class="w-full p-2 border rounded-md"
                x-ref="searchInput"
                x-on:click.stop
            >
        </div>
        
        <!-- Lista de países -->
        <div class="overflow-y-auto max-h-60">
            @forelse($countries as $code => $name)
                <button
                    type="button"
                    wire:click="selectCountry('{{ $code }}')"
                    class="flex items-center w-full px-4 py-2 text-left hover:bg-gray-100"
                >
                    {{ $this->formattedCountry($code, $name) }}
                </button>
            @empty
                <div class="px-4 py-2 text-gray-500">
                    No se encontraron resultados
                </div>
            @endforelse
        </div>
    </div>
    
    <!-- Input hidden para el formulario -->
    <input type="hidden" name="country" value="{{ $selectedCountry }}">
</div>