import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static values = {
    debounce: { type: Number, default: 250 }
  }

  connect() {
    console.log("Search controller connected", this.element)
    this.submitTimeoutId = null
  }

  queueSubmit(event) {
    console.log("queueSubmit fired", event.target.value)
    clearTimeout(this.submitTimeoutId)
    this.submitTimeoutId = setTimeout(() => this.submit(), this.debounceValue)
  }

  submit() {
    console.log("submit() called")
    this.element.requestSubmit()
  }
}
