import PlaceablePaletteMixin from "./placeable-palette-mixin.mjs";
import NoteConfig from "../note-config.mjs";

/**
 * A dialog that provides bulk operation or default values for newly-created notes.
 * @extends {NoteConfig}
 * @mixes PlaceablePaletteMixin
 */
export default class NotePalette extends PlaceablePaletteMixin(NoteConfig) {
  /** @override */
  static DEFAULT_OPTIONS = {
    id: "note-palette"
  };

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

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

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

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

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

  /** @override */
  static TABS = {};

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

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

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

  /** @override */
  static get schema() {
    const schema = super.schema;
    for ( const f of ["x", "y", "sort", "author", "entryId", "pageId"] ) {
      delete schema.fields[f];
    }
    return schema;
  }

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

  /** @override */
  async _preparePartContext(partId, context, options) {
    context.partId = `${this.id}-${partId}`;
    return context;
  }

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

  /** @inheritDoc */
  _onChangeForm(formConfig, event) {
    if ( this.isSelect ) {
      const name = event.target.name;
      if ( name.startsWith("icon.") ) this._dirtyFields.add("texture.src");
    }
    return super._onChangeForm(formConfig, event);
  }

  /* -------------------------------------------- */
  /*  Helpers                                     */
  /* -------------------------------------------- */

  /** @inheritDoc */
  _determineMultiFields(docs) {
    const multiple = super._determineMultiFields(docs);
    if ( multiple.has("texture.src") ) {
      multiple.add("icon.selected");
      multiple.add("icon.custom");
    }
    return multiple;
  }
}
