import HandlebarsApplicationMixin from "../api/handlebars-application.mjs";
import DocumentSheetV2 from "../api/document-sheet.mjs";

/**
 * The Application responsible for configuring a single Level document.
 * @extends {DocumentSheetV2}
 * @mixes HandlebarsApplication
 */
export default class LevelConfig extends HandlebarsApplicationMixin(DocumentSheetV2) {
  /** @override */
  static DEFAULT_OPTIONS = {
    classes: ["level-config"],
    window: {
      contentClasses: ["standard-form"],
      icon: "fa-solid fa-layer-group"
    },
    position: { width: 560 },
    form: { closeOnSubmit: true }
  };

  /* -------------------------------------------- */

  /** @override */
  static PARTS = {
    body: { template: "templates/scene/level/body.hbs", scrollable: [""] },
    footer: { template: "templates/generic/form-footer.hbs" }
  };

  /* -------------------------------------------- */
  /*  Rendering                                   */
  /* -------------------------------------------- */

  /** @inheritDoc */
  async _prepareContext(options) {
    const context = await super._prepareContext(options);
    context.gridUnits = this.document.parent?.grid.units || _loc("MEASUREMENT.GridUnits");
    context.buttons = [{ type: "submit", icon: "fa-solid fa-floppy-disk", label: "SETTINGS.Save" }];
    context.otherLevels = this.document.parent?.levels.reverseSorted
      .filter(l => l !== this.document)
      .map(l => {
        const { id, name } = l;
        return { label: name, value: id };
      });
    context.textureFitModes = CONST.TEXTURE_DATA_FIT_MODES.reduce((obj, fit) => {
      obj[fit] = _loc(`TEXTURE_DATA.FIT.${fit}`);
      return obj;
    }, {});
    return context;
  }

  /* -------------------------------------------- */
  /*  Form Submission                             */
  /* -------------------------------------------- */

  /** @inheritDoc */
  _prepareSubmitData(event, form, formData, updateData) {
    const submitData = super._prepareSubmitData(event, form, formData, updateData);
    submitData.background ||= null;
    submitData.foreground ||= null;
    submitData.fog ||= null;
    return submitData;
  }
}
