import PlaceableFilter from "./placeable-filter.mjs";

/**
 * An advanced filter dialog for the Regions placeable tab.
 * @extends {PlaceableFilter}
 */
export default class RegionFilter extends PlaceableFilter {
  /** @override */
  static DEFAULT_OPTIONS = {
    id: "region-filter"
  };

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

  /** @override */
  static PARTS = {
    body: {
      root: true,
      classes: ["standard-form"],
      template: "templates/sidebar/filters/region.hbs"
    }
  };

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

  /** @inheritDoc */
  async _prepareContext(options) {
    const context = await super._prepareContext(options);
    const { StringField } = foundry.data.fields;
    const choices = Object.fromEntries(
      Object.entries(CONFIG.RegionBehavior.typeLabels)
        .filter(([k]) => k !== CONST.BASE_DOCUMENT_TYPE)
    );
    const { behaviorType } = this.tab._filterState;
    context.behaviorType = {
      field: new StringField({ choices, label: "DOCUMENT.RegionBehavior" }, { name: "behaviorType" }),
      value: behaviorType
    };
    return context;
  }

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

  /** @inheritDoc */
  _onChangeForm(formConfig, event) {
    const { target } = event;
    if ( target.name === "behaviorType" ) {
      this.tab._filterState.behaviorType = target.value;
      this.tab._applyFilters();
    }
  }
}
