import { proxyCustomElement, HTMLElement, createEvent, h, Fragment, Host } from '@stencil/core/internal/client';
import { StringUtils, ObjectUtils, ApplicationContext, ElementIDUtils } from '@sankhyalabs/core';
import { F as FilterGroupCondition, D as DefaultFilter, P as PersonalizedFilterUtils } from './PersonalizedFilterUtils.js';
import { P as PersonalizedFilterFetcher, C as ConfigStorage } from './ConfigStorage.js';
import { ApplicationUtils } from '@sankhyalabs/ezui/dist/collection/utils';
import { s as store } from './index2.js';
import { d as defineCustomElement$7 } from './snk-expression-group2.js';
import { d as defineCustomElement$6 } from './snk-expression-item2.js';
import { d as defineCustomElement$5 } from './snk-filter-advanced-mode2.js';
import { d as defineCustomElement$4 } from './snk-filter-assistent-mode2.js';
import { d as defineCustomElement$3 } from './snk-filter-field-search2.js';
import { d as defineCustomElement$2 } from './snk-filter-param-config2.js';
import { d as defineCustomElement$1 } from './snk-simple-bar2.js';

const snkPersonalizedFilterCss = ".sc-snk-personalized-filter-h{display:flex;height:100%;padding:var(--space--large, 24px);--snk-personalized-filter--width:100%;--snk-personalized-filter--height:100%;--snk-personalized-filter--background-color-line:var(--color--disable-secondary);--snk-personalized-filter--margin-line:var(--border--radius-medium, 12px)}.snk-personalized-filter.sc-snk-personalized-filter{height:var(--snk-personalized-filter--width);width:var(--snk-personalized-filter--height)}.snk-personalized-filter__header-actions.sc-snk-personalized-filter{display:flex;gap:var(--space--medium)}.snk-personalized-filter__group.sc-snk-personalized-filter{flex:1}.snk-personalized-filter__expression-input--line.sc-snk-personalized-filter{width:100%;height:2px;background-color:var(--snk-personalized-filter--background-color-line);margin-top:var(--snk-personalized-filter--margin-line);margin-bottom:var(--snk-personalized-filter--margin-line)}.ez-box__main.sc-snk-personalized-filter{height:90%}.snk-personalized-filter__button-mode.sc-snk-personalized-filter{height:35px}";

const SnkPersonalizedFilter = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.ezCancel = createEvent(this, "ezCancel", 7);
    this.ezSave = createEvent(this, "ezSave", 7);
    this.ezAfterSave = createEvent(this, "ezAfterSave", 7);
    this._personalizedFilterFetcher = new PersonalizedFilterFetcher();
    this._filterMetadataStorage = new Map();
    this._dataSourceFetcher = {
      fetchData: async (entity) => {
        let entityURI = entity === null || entity === void 0 ? void 0 : entity.uri;
        if (StringUtils.isEmpty(entityURI)) {
          entityURI = this.entityUri;
        }
        if (!this._filterMetadataStorage.has(entityURI)) {
          const filterMetadata = await this._personalizedFilterFetcher.loadFields(entityURI);
          this._filterMetadataStorage.set(entityURI, filterMetadata);
        }
        return this._filterMetadataStorage.get(entityURI);
      }
    };
    this._filterAssistentMode = true;
    this._filterAssistent = undefined;
    this.messagesBuilder = undefined;
    this.entityUri = undefined;
    this.filterId = undefined;
    this.configName = undefined;
    this.resourceID = undefined;
    this.isDefaultFilter = false;
  }
  /**
   * Cria um novo filtro personalizado caso não exista nenhum.
   * @method
   * @returns {Promise<void>}
   */
  async createPersonalizedFilter() {
    const newPersonalizedFilter = {
      name: "",
      active: true,
      expression: "",
      parameters: [],
      assistent: {
        id: StringUtils.generateUUID(),
        items: [
          {
            id: StringUtils.generateUUID(),
            entityName: "",
            fieldName: "",
            description: "",
            fieldLabel: this.getMessage("snkPersonalizedFilter.info.labelDefaultValue"),
            type: undefined,
            expression: undefined,
            operand: undefined,
            paramVariable: false,
            value: undefined,
            systemConfig: null,
            userConfig: null
          }
        ],
        childrenGroups: [],
        operand: FilterGroupCondition.AND
      }
    };
    if (this.isDefaultFilter) {
      this.buildDataDefaultFilter(newPersonalizedFilter);
    }
    this._originalFilterAssistent = ObjectUtils.copy(newPersonalizedFilter);
    this._filterAssistent = ObjectUtils.copy(newPersonalizedFilter);
  }
  /**
   * Carrega o filtro personalizado com base no identificador informado.
   * @watch filterId
   * @param newValue Novo valor do filtroId.
   * @param oldValue Valor anterior do filtroId.
   */
  loadFilter(newValue, oldValue) {
    if (newValue == null || oldValue == newValue || this.resourceID == undefined) {
      return;
    }
    if (newValue === DefaultFilter.id) {
      ConfigStorage.loadDefaultFilter(newValue, this.resourceID, this.configName)
        .then((resp) => {
        const personalizedFilter = this.applyDefaultValues(resp);
        this._filterAssistent = personalizedFilter;
        this._originalFilterAssistent = ObjectUtils.copy(personalizedFilter);
        this._filterAssistentMode = this._filterAssistent.hasOwnProperty("assistent") && this._filterAssistent.assistent != undefined;
      });
      return;
    }
    ConfigStorage.loadPersonalizedFilter(newValue, this.resourceID, this.configName)
      .then((resp) => {
      const personalizedFilter = this.applyDefaultValues(resp);
      this._filterAssistent = personalizedFilter;
      this._originalFilterAssistent = ObjectUtils.copy(personalizedFilter);
      this._filterAssistentMode = this._filterAssistent.hasOwnProperty("assistent") && this._filterAssistent.assistent != undefined;
    });
  }
  componentWillLoad() {
    var _a;
    if (store.get(`filterFieldsDataSource.${this.entityUri}`) == undefined) {
      store.set(`filterFieldsDataSource.${this.entityUri}`, this._dataSourceFetcher);
    }
    if (!this.messagesBuilder) {
      this._application = ApplicationContext.getContextValue("__SNK__APPLICATION__");
      this.messagesBuilder = (_a = this._application) === null || _a === void 0 ? void 0 : _a.messagesBuilder;
    }
    this.loadFilter(this.filterId, undefined);
  }
  componentDidRender() {
    var _a;
    (_a = this._viewStackRef) === null || _a === void 0 ? void 0 : _a.show(this._filterAssistentMode ? 0 : 1);
    if (this._elButtonMode) {
      this._elButtonMode.setAttribute(ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME, ElementIDUtils.getInternalIDInfo(`${this._filterAssistentMode ? "advancedMode_ezButton" : "assistentMode_ezButton"}`));
    }
  }
  buildDataDefaultFilter(data) {
    data.name = DefaultFilter.name;
    data.id = DefaultFilter.id;
  }
  getMessage(key, params) {
    return this._application.messagesBuilder.getMessage(key, params);
  }
  async saveFilter() {
    let response;
    if (this.isDefaultFilter) {
      response = await ConfigStorage.saveDefaultFilter(this._filterAssistent, this.resourceID, this.configName);
    }
    else {
      response = await ConfigStorage.savePersonalizedFilter(this._filterAssistent, this.resourceID, this.configName);
    }
    this._elButtonSave.enabled = false;
    const messageToken = !!this._filterAssistent.id ? 'confirmEdit' : 'confirmSave';
    ApplicationUtils.info(this.getMessage(`snkPersonalizedFilter.${messageToken}.title`), { iconName: "check" });
    const updatedFilterAssistent = Object.assign(Object.assign({}, response), this._filterAssistent);
    updatedFilterAssistent.parameters = PersonalizedFilterUtils.buildVariableParameters(this._filterAssistent.assistent);
    this._originalFilterAssistent = ObjectUtils.copy(updatedFilterAssistent);
    this._filterAssistent = ObjectUtils.copy(updatedFilterAssistent);
    this.ezAfterSave.emit();
  }
  removeFilter() {
    ConfigStorage.removePersonalizedFilter(this._filterAssistent, this.resourceID, this.configName);
  }
  hasChangesToSave() {
    return JSON.stringify(this._filterAssistent) === JSON.stringify(this._originalFilterAssistent);
  }
  async handleSave() {
    if (this._filterAssistentMode) {
      const isValid = PersonalizedFilterUtils.validateFields(this._filterAssistent.assistent);
      if (!isValid || !this._filterAssistent.name)
        return this._application.error(this.getMessage("snkPersonalizedFilter.error.title"), this.getMessage("snkPersonalizedFilter.error.description"));
      this.saveAssistentMode();
    }
    else {
      if (!this._filterAssistent.expression || !this._filterAssistent.name)
        return this._application.error(this.getMessage("snkPersonalizedFilter.error.title"), this.getMessage("snkPersonalizedFilter.error.description"));
      const isValidExpressionForAdvancedMode = await this.validateExpressionBeforeSaving();
      if (!isValidExpressionForAdvancedMode)
        return;
      this.saveAdvancedMode();
    }
  }
  saveAssistentMode() {
    const { assistent, expression } = this._filterAssistent;
    const expressionQuery = !!assistent ? PersonalizedFilterUtils.buildGroupExpression(assistent) : expression;
    this._filterAssistent.expression = expressionQuery;
    this.ezSave.emit(expressionQuery);
    this.saveFilter();
  }
  saveAdvancedMode() {
    if (this._originalFilterAssistent.expression !== this._filterAssistent.expression && this._filterAssistent.assistent) {
      ApplicationUtils.confirm(this.getMessage("snkPersonalizedFilter.confirmSaveModeAdvanced.title"), this.getMessage("snkPersonalizedFilter.confirmSaveModeAdvanced.description"))
        .then((confirm) => {
        if (!confirm)
          return;
        this.saveAndTransformToAdvancedMode();
      });
    }
    else if (!this._filterAssistent.assistent) {
      this.saveAndTransformToAdvancedMode();
    }
    else {
      this.saveAssistentMode();
    }
  }
  saveAndTransformToAdvancedMode() {
    this._filterAssistent.assistent = undefined;
    this.ezSave.emit(this._filterAssistent.expression);
    this.saveFilter();
  }
  async validateExpressionBeforeSaving() {
    const { valid, message = "" } = await this._personalizedFilterFetcher.validatePersonalizedFilter(this.entityUri, this._filterAssistent.expression);
    valid || this._application.error(this.getMessage("snkPersonalizedFilter.errorValidation.title"), message.replace(/.*Exception: /, ''));
    return valid;
  }
  handleCancel() {
    if (this.hasChangesToSave()) {
      return this.ezCancel.emit();
    }
    ApplicationUtils.confirm(this.getMessage("snkPersonalizedFilter.confirmCancel.title"), this.getMessage("snkPersonalizedFilter.confirmCancel.description"))
      .then((canCancel) => {
      var _a;
      if (!canCancel)
        return;
      this._filterAssistent = ObjectUtils.copy((_a = this._originalFilterAssistent) !== null && _a !== void 0 ? _a : {});
      this.ezCancel.emit();
      this._elButtonSave.enabled = false;
    });
  }
  handleTitleChange(name) {
    this._filterAssistent = Object.assign(Object.assign({}, this._filterAssistent), { name });
    this._elButtonSave.enabled = !this.hasChangesToSave();
  }
  applyDefaultValues(personalizedFilter) {
    this.addLabelToItems(personalizedFilter);
    this.addFilterGroupCondition(personalizedFilter);
    return personalizedFilter;
  }
  addFilterGroupCondition(personalizedFilter) {
    if (personalizedFilter.assistent && !personalizedFilter.assistent.operand) {
      personalizedFilter.assistent.operand = FilterGroupCondition.AND;
    }
  }
  ;
  addLabelToItems(personalizedFilter) {
    //Workaround: Alguns filtros foram salvos sem o fieldLabel. Para evitar transtornos
    //tentamos usar o fieldLabel que pode estar informado no parametro.
    if (personalizedFilter.assistent && personalizedFilter.assistent.items && personalizedFilter.assistent.items.length > 0) {
      personalizedFilter.assistent.items.forEach(item => {
        if (item.fieldLabel == undefined) {
          const param = (personalizedFilter.parameters || []).find(param => param.fieldName === item.fieldName);
          if (param) {
            item.fieldLabel = param.label;
          }
        }
      });
    }
  }
  handleExpressionChangeAdvancedMode(expression) {
    this._filterAssistent = Object.assign(Object.assign({}, this._filterAssistent), { expression });
    this._elButtonSave.enabled = !this.hasChangesToSave();
  }
  handleChangeFilterAssistentMode(filterAssistent) {
    this._elButtonSave.enabled = !this.hasChangesToSave();
    this._filterAssistent = Object.assign({}, filterAssistent);
  }
  selectField(fieldName) {
    if (!('name' in fieldName))
      return;
    const fieldExpression = this.buildExpression(fieldName.entityName, fieldName.name, fieldName.entityPath);
    const textAreaElement = this._elAdvancedMode.querySelector('ez-text-area');
    textAreaElement.appendTextToSelection(fieldExpression);
  }
  buildExpression(entityName, fieldName, entityPath) {
    if (entityPath.length > 0) {
      entityPath.shift();
    }
    return entityPath.length > 0 ? `${entityPath.join("->")}->${fieldName}` : `${entityName}.${fieldName}`;
  }
  onAddField(target) {
    const textAreaElement = this._elAdvancedMode.querySelector('ez-text-area');
    const options = { horizontalGap: 0, verticalGap: 12, fromRight: true };
    textAreaElement.setFocus();
    target.setBlur();
    if (this._elFilterFieldSearch)
      this._elFilterFieldSearch.show(target, options);
  }
  renderButtonAddField() {
    if (this._filterAssistentMode)
      return;
    return (h(Fragment, null, h("ez-button", { class: "snk-personalized-filter__button-mode", label: this.getMessage("snkPersonalizedFilter.info.addField"), enabled: true, onClick: (evt) => this.onAddField(evt.target) }, h("ez-icon", { class: "ez-padding-right--small", slot: "leftIcon", iconName: "plus" })), h("snk-filter-field-search", { class: "ez-padding-left--medium", ref: ref => this._elFilterFieldSearch = ref, onEzSelectFilterItem: (evt) => this.selectField(evt.detail), fieldsDataSource: this._dataSourceFetcher })));
  }
  buildContainerPersonalizedFilter() {
    return (h("ez-view-stack", { ref: (ref) => this._viewStackRef = ref }, h("stack-item", null, this._filterAssistentMode &&
      h("snk-filter-assistent-mode", { filterAssistent: this._filterAssistent, messagesBuilder: this.messagesBuilder, filterId: this.filterId, entityUri: this.entityUri, application: this._application, onEzChangeFilter: (ev) => this.handleChangeFilterAssistentMode(ev.detail), isDefaultFilter: this.isDefaultFilter })), h("stack-item", null, h("snk-filter-advanced-mode", { ref: (ref) => this._elAdvancedMode = ref, filterAssistent: this._filterAssistent, application: this._application, onEzExpressionChange: (ev) => this.handleExpressionChangeAdvancedMode(ev.detail) }))));
  }
  handleModeChange() {
    var _a, _b;
    if (this._filterAssistentMode) {
      ApplicationUtils.confirm(this.getMessage("snkPersonalizedFilter.confirmChangeModeAssistent.title"), this.getMessage("snkPersonalizedFilter.confirmChangeModeAssistent.description"))
        .then((canCancel) => {
        if (!canCancel)
          return;
        this._filterAssistentMode = !this._filterAssistentMode;
        if (!this.hasChangesToSave()) {
          const expression = PersonalizedFilterUtils.buildGroupExpression(this._filterAssistent.assistent);
          this._filterAssistent.expression = expression;
          this._elButtonSave.enabled = !this.hasChangesToSave();
        }
      });
    }
    else {
      if (((_a = this._filterAssistent) === null || _a === void 0 ? void 0 : _a.expression) === ((_b = this._originalFilterAssistent) === null || _b === void 0 ? void 0 : _b.expression)) {
        this._filterAssistentMode = !this._filterAssistentMode;
      }
      else {
        const expressionBefore = this._filterAssistent.expression;
        const expression = PersonalizedFilterUtils.buildGroupExpression(this._filterAssistent.assistent);
        if (expressionBefore !== expression) {
          ApplicationUtils.confirm(this.getMessage("snkPersonalizedFilter.confirmChangeModeAdvanced.title"), this.getMessage("snkPersonalizedFilter.confirmChangeModeAdvanced.description"))
            .then((canCancel) => {
            if (!canCancel)
              return;
            this._filterAssistent = Object.assign(Object.assign({}, this._filterAssistent), { expression });
            this._filterAssistentMode = !this._filterAssistentMode;
          });
        }
        else {
          this._filterAssistentMode = !this._filterAssistentMode;
        }
      }
    }
  }
  buildTitle() {
    if (this.isDefaultFilter && !this.filterId) {
      return this.getMessage("snkPersonalizedFilter.info.titleCreateDefault");
    }
    if (this.isDefaultFilter && this.filterId) {
      return this.getMessage("snkPersonalizedFilter.info.titleEditDefault");
    }
    if (this.filterId) {
      return this.getMessage("snkPersonalizedFilter.info.titleEdit");
    }
    return this.getMessage("snkPersonalizedFilter.info.titleAdd");
  }
  render() {
    var _a, _b, _c, _d;
    if (!((_a = this._filterAssistent) === null || _a === void 0 ? void 0 : _a.assistent) && this._filterAssistentMode) {
      return;
    }
    return (h(Host, null, h("div", { class: "snk-personalized-filter" }, h("div", null, h("snk-simple-bar", { label: this.buildTitle(), onExit: () => this.handleCancel() }, h("div", { class: "snk-personalized-filter__header-actions", slot: "rightSlot" }, h("ez-button", { size: "small", label: this.getMessage("snkPersonalizedFilter.info.labelCancel"), onClick: () => this.handleCancel() }), h("ez-button", { size: "small", ref: (el) => this._elButtonSave = el, enabled: false, class: "ez-button--primary", label: this.getMessage("snkPersonalizedFilter.info.labelSave"), onClick: this.handleSave.bind(this) })))), h("div", { class: "ez-box ez-margin-bottom--medium ez-box__main" }, h("div", { class: "ez-box__container" }, h("div", { class: "ez-flex ez-flex--column ez-size-width--full" }, h("div", { class: "ez-flex" }, h("ez-tooltip", { message: this.getMessage("snkPersonalizedFilter.info.textInputDisabled"), active: this.isDefaultFilter }, h("ez-text-input", { class: "ez-padding-right--medium", label: this.getMessage("snkPersonalizedFilter.info.labelNameFilter"), value: (_b = this._filterAssistent) === null || _b === void 0 ? void 0 : _b.name, onEzChange: (evt) => this.handleTitleChange(evt.detail), enabled: !this.isDefaultFilter })), this.renderButtonAddField(), h("ez-button", { ref: (el) => this._elButtonMode = el, class: "snk-personalized-filter__button-mode", label: this._filterAssistentMode ? this.getMessage("snkPersonalizedFilter.info.activeModeAdvanced") : this.getMessage("snkPersonalizedFilter.info.activeModeAssistent"), onClick: () => this.handleModeChange(), "data-tooltip": !(this._filterAssistentMode || ((_c = this._filterAssistent) === null || _c === void 0 ? void 0 : _c.assistent)) ? this.getMessage("snkPersonalizedFilter.info.tooltipDisabledAssistentMode") : undefined, "data-flow": "bottom", enabled: (!this._filterAssistent.name || Boolean(this._filterAssistentMode || ((_d = this._filterAssistent) === null || _d === void 0 ? void 0 : _d.assistent))) })), this.buildContainerPersonalizedFilter()))))));
  }
  static get watchers() { return {
    "filterId": ["loadFilter"]
  }; }
  static get style() { return snkPersonalizedFilterCss; }
}, [2, "snk-personalized-filter", {
    "messagesBuilder": [1040],
    "entityUri": [1025, "entity-uri"],
    "filterId": [1025, "filter-id"],
    "configName": [1025, "config-name"],
    "resourceID": [1, "resource-i-d"],
    "isDefaultFilter": [4, "is-default-filter"],
    "_filterAssistentMode": [32],
    "_filterAssistent": [32],
    "createPersonalizedFilter": [64]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["snk-personalized-filter", "snk-expression-group", "snk-expression-item", "snk-filter-advanced-mode", "snk-filter-assistent-mode", "snk-filter-field-search", "snk-filter-param-config", "snk-simple-bar"];
  components.forEach(tagName => { switch (tagName) {
    case "snk-personalized-filter":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, SnkPersonalizedFilter);
      }
      break;
    case "snk-expression-group":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "snk-expression-item":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "snk-filter-advanced-mode":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "snk-filter-assistent-mode":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "snk-filter-field-search":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "snk-filter-param-config":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
    case "snk-simple-bar":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}

export { SnkPersonalizedFilter as S, defineCustomElement as d };
