import PlaceablePaletteMixin from "./placeable-palette-mixin.mjs";
import AmbientSoundConfig from "../ambient-sound-config.mjs";
import FormDataExtended from "../../ux/form-data-extended.mjs";

/**
 * A dialog that provides bulk operation or default values for newly-created ambient sounds.
 * @extends {AmbientSoundConfig}
 * @mixes PlaceablePaletteMixin
 */
export default class AmbientSoundPalette extends PlaceablePaletteMixin(AmbientSoundConfig) {
  /** @override */
  static DEFAULT_OPTIONS = {
    id: "ambient-sound-palette"
  };

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

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

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

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

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

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

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

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

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

  /** @inheritDoc */
  get createData() {
    if ( !this.rendered ) return super.createData;
    const formData = new FormDataExtended(this.form);
    return foundry.utils.expandObject(formData.object);
  }

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

  /** @override */
  static get schema() {
    const schema = super.schema;
    schema.removeFields(["x", "y", "radius", "name", "path", "locked"]);
    return schema;
  }

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

  /** @inheritDoc */
  async _prepareContext(options) {
    // Restore fields excluded from the palette schema for select mode display
    if ( this.isSelect ) this.document.updateSource({ radius: this.controlled.at(0)?._source.radius ?? 0 });
    return super._prepareContext(options);
  }

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

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