'use strict';

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

const index = require('./index-a7b0c73d.js');
const core = require('@sankhyalabs/core');
const constants = require('./constants-72b7e05e.js');
const index$1 = require('./index-1be40f19.js');

const filterColumnCss = ".filter-column{padding:var(--space--2xs, 8px);position:relative}.filter-column__header{margin-bottom:var(--space--2xs, 8px);display:flex;flex-direction:row;align-items:center;justify-content:space-between;height:var(--space--xl, 32px)}.filter-column__footer{position:relative;display:flex;flex-direction:row;justify-content:flex-end;align-items:center;width:100%;bottom:0;left:0;padding-top:var(--space--2xs, 8px);border-top:var(--border--medium) var(--color--disable-secondary)}.filter-column__popover{position:absolute}.filter-column__header-title{margin-left:8px}#filter-column-selection-list{--ez-multi-selection-default-height:unset}";

const windowInstace = window;
const onSubmitFilter = new CustomEvent('onSubmitFilter', {
  detail: {
    showToast: true,
  },
});
const FilterColumn = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.applyFilterColumnOptions = index.createEvent(this, "applyFilterColumnOptions", 7);
    this.opened = true;
    this.columnName = undefined;
    this.columnLabel = undefined;
    this.gridHeaderHidden = false;
    this.noHeaderTaskBar = false;
    this.dataSource = undefined;
    this.dataUnit = undefined;
    this.options = undefined;
    this.selectedItems = undefined;
    this.fieldDescriptor = undefined;
    this.useOptions = false;
    this.isTextSearch = false;
  }
  async hide() {
    await this.ezPopoverElement.hide();
    await this.clearConfigs();
  }
  /**
   * Abre o filtro de coluna.
   * @Params IFilterColumnConfigs informações para configurar o filtro
   */
  async show(configs) {
    var _a;
    if (this.canShow(configs)) {
      this.fieldDescriptor = (_a = configs.fieldDescriptor) !== null && _a !== void 0 ? _a : this.dataUnit.getField(configs.columnName);
      this.buildIsTextSearch();
      this.columnName = configs.columnName;
      this.columnLabel = configs.columnLabel;
      await this.setOptions(configs);
      await this.ezPopoverElement.showUnder(`filter-column-${configs.columnName}-${this.dataUnit.dataUnitId}`);
    }
  }
  buildIsTextSearch() {
    var _a, _b;
    const userInterface = (_b = (_a = this.fieldDescriptor) === null || _a === void 0 ? void 0 : _a.userInterface) !== null && _b !== void 0 ? _b : core.UserInterface.SHORTTEXT;
    this.isTextSearch = !(userInterface === core.UserInterface.DECIMALNUMBER || userInterface === core.UserInterface.INTEGERNUMBER);
  }
  async clearConfigs() {
    var _a;
    await ((_a = this.ezMultiSelectionList) === null || _a === void 0 ? void 0 : _a.clearFilteredOptions());
    this.options = null;
    this.columnName = null;
  }
  canShow(configs) {
    return !this.ezPopoverElement.opened || configs.columnName !== this.columnName;
  }
  submit() {
    this.applyFilterColumnOptions.emit({
      field: this.columnName,
      values: this.selectedItems,
    });
    windowInstace.dispatchEvent(onSubmitFilter);
    this.hide();
  }
  changeSelectedItems(items) {
    this.selectedItems = items;
  }
  hasToUseOptions() {
    return this.fieldDescriptor.userInterface === core.UserInterface.OPTIONSELECTOR
      || this.fieldDescriptor.userInterface === core.UserInterface.SWITCH
      || this.fieldDescriptor.userInterface === core.UserInterface.CHECKBOX;
  }
  isCheckBoxOrSwitch() {
    return this.fieldDescriptor.userInterface === core.UserInterface.SWITCH
      || this.fieldDescriptor.userInterface === core.UserInterface.CHECKBOX;
  }
  async setOptions(config) {
    var _a, _b, _c, _d;
    const selectedItems = config.filteredOptions;
    const validateFieldUserInterfaceOptionSelector = this.hasToUseOptions();
    if (validateFieldUserInterfaceOptionSelector) {
      this.useOptions = validateFieldUserInterfaceOptionSelector;
      this.options = validateFieldUserInterfaceOptionSelector ? this.buildOptions(selectedItems) : (selectedItems !== null && selectedItems !== void 0 ? selectedItems : null);
      return;
    }
    let colName = this.columnName;
    const staticOptionsConfig = { onlyLabel: false };
    if ((_b = (_a = config.fieldDescriptor) === null || _a === void 0 ? void 0 : _a.properties) === null || _b === void 0 ? void 0 : _b[constants.PRESENTATION_FROM_COL_PROP_NAME]) {
      colName = (_d = (_c = config.fieldDescriptor) === null || _c === void 0 ? void 0 : _c.properties) === null || _d === void 0 ? void 0 : _d[constants.PRESENTATION_FROM_COL_PROP_NAME];
      staticOptionsConfig.onlyLabel = true;
    }
    const options = await this.dataSource.getStaticOptions(colName, staticOptionsConfig);
    if (options != undefined) {
      const selected = new Set((selectedItems || []).filter(item => item.check).map(item => item.label));
      this.useOptions = true;
      this.options = selected.size === 0 ? options : options.map(option => (Object.assign(Object.assign({}, option), { check: selected.has(option.label) })));
      return;
    }
    this.useOptions = false;
    this.options = null;
  }
  buildOptions(selectedItems) {
    var _a, _b;
    if (selectedItems && selectedItems.length > 0) {
      return selectedItems;
    }
    if (this.isCheckBoxOrSwitch()) {
      return [
        { label: this.i18n("app.yes"), value: 'true', check: true },
        { label: this.i18n("app.no"), value: 'false', check: true },
      ];
    }
    const options = (_b = (_a = this.fieldDescriptor) === null || _a === void 0 ? void 0 : _a.properties) === null || _b === void 0 ? void 0 : _b.options;
    if (Array.isArray(options)) {
      return options.map((option) => {
        return { label: option.value, value: option.value, check: true };
      });
    }
    const optionsObject = JSON.parse(options);
    const objectKeys = Object.keys(optionsObject);
    return objectKeys.map(key => {
      return { label: optionsObject[key], value: key, check: true };
    });
  }
  handleEzVisibilityChange(event) {
    if (event.detail === false) {
      this.clearConfigs();
    }
  }
  async componentWillLoad() {
    this.i18n = await index$1.initI18n();
  }
  render() {
    return (index.h("ez-popover-plus", { ref: (element) => (this.ezPopoverElement = element), class: 'filter-column__popover', "overlay-type": 'none', autoClose: true, onEzVisibilityChange: this.handleEzVisibilityChange.bind(this) }, index.h("section", { class: 'filter-column' }, index.h("header", { class: 'filter-column__header' }, index.h("span", { class: 'ez-text ez-text--medium filter-column__header-title' }, this.i18n("ez-grid.filterColumnHeader", { columnLabel: this.columnLabel })), index.h("ez-button", { mode: 'icon', "icon-name": 'close', class: 'ez-button--tertiary', onClick: () => this.hide() })), index.h("ez-multi-selection-list", { id: 'filter-column-selection-list', columnName: this.columnName, dataSource: this.dataSource, options: this.options, useOptions: this.useOptions, ref: (element) => (this.ezMultiSelectionList = element), onChangeFilteredOptions: (event) => this.changeSelectedItems(event.detail), isTextSearch: this.isTextSearch, "data-element-id": this.columnName }), index.h("footer", { class: 'filter-column__footer' }, index.h("ez-button", { label: this.i18n("app.apply"), class: 'ez-button--primary', onClick: this.submit.bind(this) })))));
  }
};
FilterColumn.style = filterColumnCss;

exports.filter_column = FilterColumn;
