import $ from 'jquery';

class FillableFields {
  constructor(element) {
    this.element = element
    this.target = document.querySelector(element.dataset.target)
  }

  fill() {
    const fillables = this.element.querySelectorAll("[data-fillable-field]")

    fillables.forEach((fillable) => {
      const selector = `input[name='${fillable.dataset.fillableField}']`
      const fillableTarget = this.target.querySelector(selector)
      fillableTarget.value = fillable.textContent
    })
  }
}

export default FillableFields

document.addEventListener("turbolinks:load", function () {
  $(document).on("click", "[data-behavior=fillable-fields]", (event) => {
    new FillableFields(event.currentTarget).fill()
  });
})
