export function selectSearch(options = [], initialValue = '', placeholder = 'Search...') {
    return {
        options,
        value: String(initialValue ?? ''),
        search: '',
        open: false,
        placeholder,

        get selectedLabel() {
            const found = this.options.find(o => String(o.value) === String(this.value))
            return found ? found.label : ''
        },

        get filtered() {
            if (!this.search.trim()) return this.options
            const q = this.search.toLowerCase()
            return this.options.filter(o => o.label.toLowerCase().includes(q))
        },

        select(option) {
            this.value = String(option.value)
            this.search = ''
            this.open = false
        },

        clear() {
            this.value = ''
            this.search = ''
            this.open = false
        },

        onKeydown(e) {
            if (e.key === 'Escape') { this.open = false; this.search = '' }
        },
    }
}
