import PlaceablePaletteMixin from "./placeable-palette-mixin.mjs";
import WallConfig from "../wall-config.mjs";

/**
 * A dialog that provides bulk operation or default values for newly-created walls.
 * @extends {WallConfig}
 * @mixes PlaceablePaletteMixin
 */
export default class WallPalette extends PlaceablePaletteMixin(WallConfig) {

  /** @override */
  static DEFAULT_OPTIONS = {
    id: "wall-palette",
    initialData: { c: [0, 0, 0, 0] }
  };

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

  /** @override */
  static PARTS = {
    body: { template: "templates/scene/palette/wall/body.hbs" },
    footer: { template: "templates/generic/form-footer.hbs" }
  };

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

  /**
   * The setting key where default data is saved.
   * @type {string}
   */
  static SETTING_KEY = "wallPalette";

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

  /** @override */
  static COMMIT_TOOL = "wall";

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

  /**
   * The placeable document.
   * @type {string}
   */
  static documentName = "Wall";

  /* -------------------------------------------- */
  /*  Properties                                  */
  /* -------------------------------------------- */

  /** @inheritDoc */
  static get schema() {
    const schema = super.schema;
    delete schema.fields.c;
    return schema;
  }

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

  /** @inheritDoc */
  async _prepareContext(options) {
    const context = await super._prepareContext(options);
    delete context.coordinates;
    return context;
  }

  /* -------------------------------------------- */
  /*  Event Listeners & Handlers                  */
  /* -------------------------------------------- */

  /**
   * Handle clicking a preset button in the scene controls.
   * @param {PointerEvent} event
   */
  static onClickPreset(event) {
    const { tool } = event.target.closest("[data-tool]")?.dataset ?? {};
    const preset = foundry.utils.deepClone(ui.controls.tools[tool].createData);
    game.settings.set("core", WallPalette.SETTING_KEY, preset);
    ui.controls.render({ parts: ["tools"] });
    ui.placeablesPalette?.render({ preset, preservePlacement: true });
  }
}
