'use strict';

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

const index = require('./index-17105557.js');
const core = require('@sankhyalabs/core');
const utils = require('@sankhyalabs/ezui/dist/collection/utils');
const PersonalizedFilterUtils = require('./PersonalizedFilterUtils-eccb267a.js');
const presentationMode = require('./presentationMode-52ec3bdd.js');
const filterItemType_enum = require('./filter-item-type.enum-a7ffdaa6.js');
require('./index-62760a9c.js');

const SnkFilterDetail = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.filterChange = index.createEvent(this, "filterChange", 7);
    this.config = undefined;
    this.getMessage = undefined;
    this.showHardFixed = true;
    this.removalBlocked = false;
  }
  /**
   * Define o foco no campo do editor.
   */
  async setFocusField() {
    var _a, _b, _c, _d;
    if (!this._editor) {
      return;
    }
    if (this._editor["setFocus"]) {
      (_b = (_a = this._editor) === null || _a === void 0 ? void 0 : _a["setFocus"]) === null || _b === void 0 ? void 0 : _b.call(_a);
    }
    else if (this._editor["focus"]) {
      (_d = (_c = this._editor) === null || _c === void 0 ? void 0 : _c["focus"]) === null || _d === void 0 ? void 0 : _d.call(_c);
    }
  }
  async clearValue() {
    if (!this._editor) {
      return;
    }
    if (this._editor["clearValue"]) {
      await this._editor["clearValue"]();
    }
  }
  observeConfig(newValue) {
    var _a, _b;
    this._editor && (this._editor["config"] = newValue);
    (_b = (_a = this._editor) === null || _a === void 0 ? void 0 : _a["reloadList"]) === null || _b === void 0 ? void 0 : _b.call(_a);
    this._customElementObject = this.getContentEditor();
  }
  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.config.id}`;
    }
  }
  changeConfig(newConfig) {
    this.filterChange.emit(newConfig);
  }
  getContentEditor() {
    switch (this.config.type) {
      case filterItemType_enum.FilterItemType.BINARY_SELECT:
        return { tag: "snk-filter-binary-select" };
      case filterItemType_enum.FilterItemType.MULTI_SELECT:
        return { tag: "snk-filter-multi-select" };
      case filterItemType_enum.FilterItemType.PERIOD:
        return { tag: "snk-filter-period" };
      case filterItemType_enum.FilterItemType.SEARCH:
        return { tag: "snk-filter-search" };
      case filterItemType_enum.FilterItemType.NUMBER:
        return { tag: "snk-filter-number" };
      case filterItemType_enum.FilterItemType.PERSONALIZED:
        const snkFilterPersonalizedProps = {
          tag: "snk-personalized-filter-editor", props: {
            presentationMode: presentationMode.EPresentationMode.CHIP,
            onValueChanged: evt => this.handleValueChanged(evt),
          }
        };
        return snkFilterPersonalizedProps;
      case filterItemType_enum.FilterItemType.CHECK_BOX_LIST:
        return {
          tag: "snk-filter-checkbox-list",
          props: {
            onValueChanged: evt => this.config = Object.assign(Object.assign({}, this.config), { value: evt.detail })
          }
        };
      case filterItemType_enum.FilterItemType.MULTI_LIST:
        return {
          tag: "snk-entity-list",
          props: {
            onValueChanged: evt => this.config = Object.assign(Object.assign({}, this.config), { value: evt.detail })
          }
        };
    }
    return { tag: "snk-filter-text" };
  }
  handleValueChanged({ detail }) {
    const groupedItems = (detail === null || detail === void 0 ? void 0 : detail.items) || this.config.groupedItems;
    this.config = Object.assign(Object.assign({}, this.config), { groupedItems, visible: (groupedItems || []).filter(item => item.visible).length > 0 });
  }
  removeItem() {
    const hasMultiValues = this.config.type === filterItemType_enum.FilterItemType.MULTI_LIST && Array.isArray(this.config.value);
    const newValue = hasMultiValues ? this.config.value.map((valueItem) => (Object.assign(Object.assign({}, valueItem), { check: false }))) : undefined;
    const removedConfig = Object.assign(Object.assign({}, this.config), { visible: false, fixed: false, value: newValue });
    if (removedConfig.groupedItems != undefined) {
      removedConfig.groupedItems = removedConfig.groupedItems.map(item => (Object.assign(Object.assign({}, item), { visible: false, fixed: false, value: undefined })));
    }
    this.changeConfig(removedConfig);
  }
  removeItemFromGroup(item) {
    return Object.assign(Object.assign({}, item), { visible: false, fixed: false, value: undefined });
  }
  getPopUpHeaderButtons() {
    return (index.h(index.Fragment, null, !this.removalBlocked && !this.config.removalBlocked &&
      this.buildIcon(this.getMessage("snkFilterBar.removeFilter"), "delete", () => this.removeItem()), this.showHardFixed && !this.config.hardFixed &&
      this.buildIcon(this.getMessage(this.config.fixed ? "snkFilterBar.unpinFilter" : "snkFilterBar.pinFilter"), this.config.fixed ? "un-pin" : "push-pin", () => this.changeConfig(Object.assign(Object.assign({}, this.config), { fixed: !this.config.fixed })))));
  }
  buildIcon(title, iconName, action) {
    return (index.h("button", { onClick: () => action(), class: "sc-snk-filter-bar snk-filter-item__editor-header-button", ref: elem => elem && this.buildIdButton(elem, title) }, index.h("ez-icon", Object.assign({ title: title, iconName: iconName }, { [core.ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: `${core.ElementIDUtils.getInternalIDInfo(`_${title}`)}` }))));
  }
  buildIdButton(buttonElement, title) {
    if (!buttonElement)
      return;
    const dataElement = { id: title };
    buttonElement.removeAttribute(core.ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME);
    core.ElementIDUtils.addIDInfoIfNotExists(buttonElement, "button", dataElement);
  }
  clearBinarySelectValues() {
    var _a;
    const editor = this._editor || {};
    (_a = editor.resetValues) === null || _a === void 0 ? void 0 : _a.call(editor);
    this.config = core.ObjectUtils.copy(this.originalConfig);
    this.changeConfig(Object.assign(Object.assign({}, this.config), { value: undefined }));
  }
  apply() {
    const isValid = PersonalizedFilterUtils.PersonalizedFilterUtils.validateVariableValues(this.config, this._editor["value"]);
    if (!isValid) {
      return utils.ApplicationUtils.alert(this.getMessage('snkFilterBar.filterModal.validations.notFullFilled.title'), this.getMessage('snkFilterBar.filterModal.validations.notFullFilled.message'));
    }
    const valuesArray = this._editor["value"] !== undefined ? this._editor["value"] : this.config.value;
    this.changeConfig(Object.assign(Object.assign({}, this.config), { visible: this.isVisible(), value: valuesArray }));
  }
  isVisible() {
    if (this.config.groupedItems && this.config.groupedItems.length > 0) {
      return this.config.groupedItems.filter(item => item.visible).length > 0;
    }
    return true;
  }
  clear() {
    var _a;
    if (this.config.type === filterItemType_enum.FilterItemType.BINARY_SELECT) {
      this.clearBinarySelectValues();
      return;
    }
    if (filterItemType_enum.FilterItemType.MULTI_LIST === this.config.type) {
      const configCopy = core.ObjectUtils.copy(this.config);
      (_a = configCopy.value) === null || _a === void 0 ? void 0 : _a.forEach(value => !!value && (value.check = false));
      this.config = core.ObjectUtils.copy(configCopy);
      return;
    }
    this.changeConfig(Object.assign(Object.assign({}, this.config), { value: undefined }));
  }
  filterCanBeCleared() {
    return this.config.type !== filterItemType_enum.FilterItemType.PERSONALIZED;
  }
  onKeyDonwListener(event) {
    if (event.key === "Enter") {
      this.apply();
    }
  }
  buildRightSlot(item) {
    return index.h("ez-icon", { iconName: "delete", onClick: () => this.removeValueFromConfig(item) });
  }
  removeValueFromConfig(item) {
    var _a, _b;
    if (this.config) {
      const configCopy = core.ObjectUtils.copy(this.config);
      const index = (_a = configCopy.value) === null || _a === void 0 ? void 0 : _a.findIndex(i => (i === null || i === void 0 ? void 0 : i.id) === item.id);
      (_b = configCopy.value) === null || _b === void 0 ? void 0 : _b.splice(index, 1);
      this.config = core.ObjectUtils.copy(configCopy);
    }
  }
  componentWillLoad() {
    this.originalConfig = core.ObjectUtils.copy(this.config);
    this._customElementObject = this.getContentEditor();
  }
  render() {
    const { tag: CustomElement, props } = this._customElementObject;
    return (index.h(index.Host, null, index.h("div", { class: "col ez-col--sd-12 ez-align--right ez-padding-medium ez-flex--align-items-center" }, index.h("div", { class: "ez-text ez-title ez-title--small ez-padding--small sc-snk-filter-bar snk-filter-item__editor-header" }, this.config.detailTitle), this.getPopUpHeaderButtons()), index.h(CustomElement, Object.assign({ maxHeightList: '240px', ref: ref => this._editor = ref, value: this.config.value, config: this.config, onKeyDown: evt => this.onKeyDonwListener(evt), "data-element-id": this._idContentEditor, getMessage: this.getMessage, rightListSlotBuilder: item => this.buildRightSlot(item) }, props)), index.h("hr", { class: "sc-snk-filter-bar snk-filter__popover-rule" }), index.h("div", { class: "ez-col ez-col--sd-12 ez-align--right" }, this.filterCanBeCleared() && (index.h("ez-button", { label: this.getMessage("snkFilterBar.cleanFilter"), onClick: () => this.clear(), size: "small" })), index.h("ez-button", { ref: ref => this._applyButton = ref, label: this.getMessage("snkFilterBar.applyFilter"), onClick: () => this.apply(), size: "small", class: "ez-button--primary ez-padding-left--medium" }))));
  }
  get _element() { return index.getElement(this); }
  static get watchers() { return {
    "config": ["observeConfig"]
  }; }
};

exports.snk_filter_detail = SnkFilterDetail;
