import {Controller} from '@hotwired/stimulus';

/*
* The following line makes this controller "lazy": it won't be downloaded until needed
* See https://github.com/symfony/stimulus-bridge#lazy-controllers
*/
/* stimulusFetch: 'lazy' */
export default class extends Controller {
  static targets = ["body", "toggleIcon"]
  static values = { open: Boolean }

  connect() {
    if (!this.openValue) {
      this.close()
    }
  }

  toggle() {
    if (this.bodyTarget.style.display === "none") {
      this.open()
    } else {
      this.close()
    }
  }

  open() {
    this.bodyTarget.style.display = "block"
    this.toggleIconTarget.classList.replace("fa-chevron-down", "fa-chevron-up")
  }

  close() {
    this.bodyTarget.style.display = "none"
    this.toggleIconTarget.classList.replace("fa-chevron-up", "fa-chevron-down")
  }
}
