import { Controller } from "@hotwired/stimulus" export default class extends Controller { static targets = ["input", "results"] static values = { url: String } connect() { this.timeout = null console.log("Location autocomplete connected") } search() { clearTimeout(this.timeout) const query = this.inputTarget.value.trim() if (query.length < 1) { this.hideResults() return } this.timeout = setTimeout(() => { this.performSearch(query) }, 200) } async performSearch(query) { const url = `${this.urlValue}?q=${encodeURIComponent(query)}` try { const response = await fetch(url) const locations = await response.json() this.displayResults(locations) } catch (error) { console.error("Location search error:", error) } } displayResults(locations) { if (locations.length === 0) { this.hideResults() return } const html = locations.map(location => `