{{--
    Select input backed by a value => label map.

        <x-form.select name="status" label="Status"
                       :options="\App\Enums\AssetStatus::options()"
                       placeholder="Choose a status" required />

    Enum-backed selects should always be fed from the enum's options() method so
    that the list, the labels and the validation rule cannot drift apart.
--}}
@props([
    'name',
    'label' => null,
    'options' => [],
    'selected' => null,
    'placeholder' => null,
    'help' => null,
    'required' => false,
])

@php
    $id = $attributes->get('id', $name);
    $hasError = $errors->has($name);
    $current = old($name, $selected);
@endphp

<div class="ams-field">
    @if ($label)
        <label for="{{ $id }}" class="form-label">
            {{ $label }}@if ($required)<span class="ams-required" aria-hidden="true">*</span>@endif
        </label>
    @endif

    <select name="{{ $name }}"
            id="{{ $id }}"
            @if ($required) required @endif
            @if ($help) aria-describedby="{{ $id }}-help" @endif
            {{ $attributes->except('id')->merge(['class' => 'form-select'.($hasError ? ' is-invalid' : '')]) }}>

        @if ($placeholder !== null)
            <option value="">{{ $placeholder }}</option>
        @endif

        @foreach ($options as $optionValue => $optionLabel)
            <option value="{{ $optionValue }}" @selected((string) $current === (string) $optionValue)>
                {{ $optionLabel }}
            </option>
        @endforeach
    </select>

    @if ($help)
        <div class="form-text" id="{{ $id }}-help">{{ $help }}</div>
    @endif

    @error($name)
        <div class="invalid-feedback">{{ $message }}</div>
    @enderror
</div>
