import AwEItemSheet from "./base-item-sheet.mjs"

export default class AwEFieldSheet extends AwEItemSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    classes: ["field"],
    position: { width: 620 },
    window: { contentClasses: ["field-content"] },
    actions: {
      addSpecialization: AwEFieldSheet.#onAddSpecialization,
      removeSpecialization: AwEFieldSheet.#onRemoveSpecialization
    }
  }

  /** @override */
  static PARTS = {
    main: {
      template: "systems/fvtt-adventures-with-emmy/templates/field.hbs"
    }
  }

  /**
   * Handle adding a specialization.
   * @param {PointerEvent} event - The initiating event.
   * @param {HTMLElement} target - The input element.
   */
  static async #onAddSpecialization(event, target) {
    const value = target.value.trim()
    if (!value) return
    const current = this.document.system.specializations ?? []
    await this.document.update({ "system.specializations": [...current, value] })
    target.value = ""
  }

  /**
   * Handle removing a specialization.
   * @param {PointerEvent} event - The initiating click event.
   * @param {HTMLElement} target - The remove button.
   */
  static async #onRemoveSpecialization(event, target) {
    const index = Number(target.dataset.index)
    const current = [...(this.document.system.specializations ?? [])]
    current.splice(index, 1)
    await this.document.update({ "system.specializations": current })
  }
}
