'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-17105557.js');
const core = require('@sankhyalabs/core');
const ConfigStorage = require('./ConfigStorage-b829def9.js');
const presentationMode = require('./presentationMode-52ec3bdd.js');
const filterItemType_enum = require('./filter-item-type.enum-a7ffdaa6.js');
const filterType_enum = require('./filter-type.enum-2bb46d2a.js');
require('./form-config-fetcher-fef79845.js');
require('./DataFetcher-21f8c2dc.js');
require('./PrintUtils-bcaeb82f.js');
require('@sankhyalabs/core/dist/dataunit/metadata/UnitMetadata');

const snkFilterModalItemCss = ".grow{flex-grow:1}.gap{gap:4px}";

const SnkFilterModalItem = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.filterChange = index.createEvent(this, "filterChange", 7);
    this.editPersonalizedFilter = index.createEvent(this, "editPersonalizedFilter", 7);
    this.addPersonalizedFilter = index.createEvent(this, "addPersonalizedFilter", 7);
    this.filterItem = undefined;
    this.configName = undefined;
    this.resourceID = undefined;
  }
  getContentEditorConfig() {
    const props = { presentationMode: presentationMode.EPresentationMode.MODAL };
    switch (this.filterItem.type) {
      case filterItemType_enum.FilterItemType.BINARY_SELECT:
        return { tag: "snk-filter-binary-select", props };
      case filterItemType_enum.FilterItemType.MULTI_SELECT:
        return { tag: "snk-filter-multi-select" };
      case filterItemType_enum.FilterItemType.PERIOD:
        return { tag: "snk-filter-period", props };
      case filterItemType_enum.FilterItemType.SEARCH:
        return { tag: "snk-filter-search" };
      case filterItemType_enum.FilterItemType.NUMBER:
        return { tag: "snk-filter-number", props };
      case filterItemType_enum.FilterItemType.PERSONALIZED:
        return this.buildSnkFilterPersonalizedProps();
      case filterItemType_enum.FilterItemType.CHECK_BOX_LIST:
        return { tag: "snk-filter-checkbox-list", props };
      case filterItemType_enum.FilterItemType.MULTI_LIST:
        return { tag: "snk-entity-list", props: { maxHeightList: "640px", rightListSlotBuilder: item => this.buildRightSlot(item) } };
    }
    return { tag: "snk-filter-text" };
  }
  buildSnkFilterPersonalizedProps() {
    return {
      tag: "snk-personalized-filter-editor", props: {
        onEditFilter: (event) => this.editPersonalizedFilter.emit(event.detail),
        onAddFilter: () => this.addPersonalizedFilter.emit(),
      }
    };
  }
  isVisible(newValue) {
    return !this.filterItem.visible ? newValue !== undefined : true;
  }
  updateFilterValue(data) {
    var _a, _b;
    const newValue = (data === null || data === void 0 ? void 0 : data.value) !== undefined ? data.value : data;
    if (this.filterItem.groupedItems != undefined) {
      this.filterItem = Object.assign(Object.assign({}, this.filterItem), { visible: newValue > 0, value: newValue, groupedItems: data.items });
    }
    else {
      this.filterItem = Object.assign(Object.assign({}, this.filterItem), { value: this.filterItem.type === (filterItemType_enum.FilterItemType.SEARCH || filterItemType_enum.FilterItemType.CHECK_BOX_LIST) ? data : newValue, visible: this.isVisible(newValue) });
    }
    this._editor && (this._editor["config"] = this.filterItem);
    (_b = (_a = this._editor) === null || _a === void 0 ? void 0 : _a['reloadList']) === null || _b === void 0 ? void 0 : _b.call(_a);
    this.filterChange.emit(this.filterItem);
  }
  handleDeleteFilter(filter, filterItemType) {
    if (filterItemType === filterItemType_enum.FilterItemType.PERSONALIZED) {
      ConfigStorage.ConfigStorage.removePersonalizedFilter(filter, this.resourceID, this.configName);
    }
  }
  componentDidLoad() {
    if (this._element) {
      core.ElementIDUtils.addIDInfo(this._element);
      const dataElementIdDoFilterItem = this._element.getAttribute(core.ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME);
      this._idContentEditor = `${dataElementIdDoFilterItem}_${this.filterItem.id}`;
    }
  }
  buildRightSlot(item) {
    return (index.h("ez-icon", { iconName: "delete", onClick: () => this.removeValueFromConfig(item) }));
  }
  removeValueFromConfig(item) {
    var _a, _b, _c;
    const index = (_b = (_a = this.filterItem) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b.findIndex(({ id }) => id === item.id);
    const filterItemCopy = core.ObjectUtils.copy(this.filterItem);
    (_c = filterItemCopy === null || filterItemCopy === void 0 ? void 0 : filterItemCopy.value) === null || _c === void 0 ? void 0 : _c.splice(index, 1);
    this.updateFilterValue(filterItemCopy.value);
  }
  canShowDetailModal() {
    const { CUSTOM_FILTER, OTHER_FILTERS } = filterType_enum.FilterType;
    return ![CUSTOM_FILTER, OTHER_FILTERS].includes(this.filterItem.filterType) && this.filterItem.type !== filterItemType_enum.FilterItemType.BINARY_SELECT;
  }
  render() {
    const { tag: ContentEditor, props } = this.getContentEditorConfig();
    return (index.h(index.Host, null, index.h("div", { class: "ez-flex ez-flex--column grow" }, this.canShowDetailModal() && (index.h("label", { class: "ez-text ez-text--medium ez-text--primary ez-margin-bottom--extra-small", htmlFor: `filter-item-${this.filterItem.id}` }, this.filterItem.detailModal)), index.h(ContentEditor, Object.assign({ ref: ref => this._editor = ref, id: `filter-item-${this.filterItem.id}`, config: this.filterItem, value: this.filterItem.value, onValueChanged: (event) => this.updateFilterValue(event.detail), "data-element-id": this._idContentEditor }, props)))));
  }
  get _element() { return index.getElement(this); }
};
SnkFilterModalItem.style = snkFilterModalItemCss;

exports.snk_filter_modal_item = SnkFilterModalItem;
