// SPDX-FileCopyrightText: 2022 Johannes Loher
//
// SPDX-License-Identifier: MIT

export class DS4ActiveEffectConfig extends ActiveEffectConfig {
  /** @override */
  static get defaultOptions() {
    return foundry.utils.mergeObject(super.defaultOptions, {
      template: "systems/ds4/templates/sheets/active-effect/active-effect-config.hbs",
    });
  }

  /**
   * @override
   * @param {JQuery} html
   */
  activateListeners(html) {
    super.activateListeners(html);
    const checkbox = html[0]?.querySelector('input[name="flags.ds4.itemEffectConfig.applyToItems"]');
    checkbox?.addEventListener("change", () => this.#toggleItemEffectConfig(checkbox.checked));
  }

  /**
   * Toggle the visibility of the item effect config section
   * @param {boolean} active The target state
   */
  #toggleItemEffectConfig(active) {
    const elements = this.element[0]?.querySelectorAll(".ds4-item-effect-config");
    elements?.forEach((element) => {
      if (active) {
        element.classList.remove("ds4-hidden");
      } else {
        element.classList.add("ds4-hidden");
      }
    });
    this.setPosition({ height: "auto" });
  }
}
