import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  submit({ params }) {
    if (params.submitter) {
      this.element.requestSubmit(this.#find(params.submitter))
    } else {
      this.element.requestSubmit()
    }
  }

  #find(id) {
    return document.getElementById(id) || this.#notFound(id)
  }

  #notFound(id) {
    throw new Error(`Element with ID "${id}" not found`)
  }
}