import { r as registerInstance, h, f as Host, g as getElement } from './index-e9c7ceaa.js';
import { ObjectUtils, StringUtils, ApplicationContext, ElementIDUtils } from '@sankhyalabs/core';
import { P as PesquisaFetcher, b as buildGridMetadata, a as PESQUISA_VIEW_MODE } from './ISave-c8bf12c4.js';
import './DataFetcher-955ee53e.js';
import './index-b40568ff.js';
import '@sankhyalabs/ezui/dist/collection/utils/constants';
import '@sankhyalabs/core/dist/dataunit/metadata/UnitMetadata';
import './dataunit-fetcher-6cacb959.js';
import './filter-item-type.enum-d45e026f.js';
import './form-config-fetcher-baa89098.js';
import './constants-7b422de0.js';
import './PrintUtils-3e4ff0f5.js';
import '@sankhyalabs/ezui/dist/collection/components/ez-grid/utils';
import '@sankhyalabs/core/dist/utils/SortingUtils';
import './ResourceIDUtils-a114189a.js';

const snkPesquisaCss = ".sc-snk-pesquisa-h{--snk-pesquisa--font-size:var(--text--medium, 14px);--snk-pesquisa--font-family:var(--font-pattern, Arial);--snk-pesquisa--font-weight:var(--text-weight--medium, 400);--snk-pesquisa__records--color:var(--text--primary, #626e82);--snk-pesquisa__records--color-secondary:var(--text--secondary, #626e82);--snk-pesquisa__records--padding-vertical:var(--space--medium, 12px);--snk-pesquisa__content--padding-right:var(--space--small, 6px);--snk-pesquisa__btn--color:var(--title--primary, #2B3A54);--snk-pesquisa__btn-disabled--color:var(--text--disable, #AFB6C0);--snk-pesquisa__btn-hover--color:var(--color--primary, #4e4e4e);--snk-pesquisa__scrollbar--color-default:var(--scrollbar--default, #626e82);--snk-pesquisa__scrollbar--color-background:var(--scrollbar--background, #E5EAF0);--snk-pesquisa__scrollbar--color-hover:var(--scrollbar--hover, #2B3A54);--snk-pesquisa__scrollbar--color-clicked:var(--scrollbar--clicked, #a2abb9);--snk-pesquisa__scrollbar--border-radius:var(--border--radius-small, 6px);--snk-pesquisa__scrollbar--width:var(--space--small, 6px);height:100%;display:flex;flex-direction:column;overflow-y:auto}.snk-pesquisa.sc-snk-pesquisa{display:flex;flex-direction:column;height:100%}.snk-pesquisa__input.sc-snk-pesquisa{display:flex;width:100%;box-sizing:border-box;padding-top:4px;padding-right:4px}.snk-pesquisa__input.sc-snk-pesquisa ez-icon.sc-snk-pesquisa{--ez-icon--color:inherit;font-weight:var(--text-weight--large, 600)}.snk-pesquisa__content.sc-snk-pesquisa{display:flex;flex-direction:column;overflow-y:hidden;scrollbar-width:thin;scrollbar-color:var(--snk-pesquisa__scrollbar--color-clicked) var(--snk-pesquisa__scrollbar--color-background);padding-right:var(--snk-pesquisa__content--padding-right);height:100vh}.snk-pesquisa__content.sc-snk-pesquisa::-webkit-scrollbar{background-color:var(--snk-pesquisa__scrollbar--color-background);width:var(--snk-pesquisa__scrollbar--width);max-width:var(--snk-pesquisa__scrollbar--width);min-width:var(--snk-pesquisa__scrollbar--width)}.snk-pesquisa__content.sc-snk-pesquisa::-webkit-scrollbar-track{background-color:var(--snk-pesquisa__scrollbar--color-background);border-radius:var(--snk-pesquisa__scrollbar--border-radius)}.snk-pesquisa__content.sc-snk-pesquisa::-webkit-scrollbar-thumb{background-color:var(--snk-pesquisa__scrollbar--color-default);border-radius:var(--snk-pesquisa__scrollbar--border-radius)}.snk-pesquisa__content.sc-snk-pesquisa::-webkit-scrollbar-thumb:vertical:hover,.snk-pesquisa__content.sc-snk-pesquisa::-webkit-scrollbar-thumb:horizontal:hover{background-color:var(--snk-pesquisa__scrollbar--color-hover)}.snk-pesquisa__content.sc-snk-pesquisa::-webkit-scrollbar-thumb:vertical:active,.snk-pesquisa__content.sc-snk-pesquisa::-webkit-scrollbar-thumb:horizontal:active{background-color:var(--snk-pesquisa__scrollbar--color-clicked)}.snk-pesquisa__records-label.sc-snk-pesquisa{display:flex;flex-direction:row;justify-content:space-between;font-family:var(--snk-pesquisa--font-family);font-weight:var(--snk-pesquisa--font-weight);font-size:var(--snk-pesquisa--font-size);padding-bottom:var(--snk-pesquisa__records--padding-vertical);padding-top:var(--snk-pesquisa__records--padding-vertical)}.snk-pesquisa__records-label.hidden.sc-snk-pesquisa{display:none}.snk-pesquisa__records.sc-snk-pesquisa{color:var(--snk-pesquisa__records--color)}.snk-pesquisa__records-limit.sc-snk-pesquisa{color:var(--snk-pesquisa__records--color-secondary)}.snk-pesquisa__records-limit.hidden.sc-snk-pesquisa,.snk-pesquisa__records.hidden.sc-snk-pesquisa{visibility:hidden}.snk-pesquisa__btn.sc-snk-pesquisa{outline:none;border:none;background:none;cursor:pointer;color:var(--snk-pesquisa__btn--color)}.snk-pesquisa__btn.sc-snk-pesquisa:disabled{cursor:unset;color:var(--snk-pesquisa__btn-disabled--color)}.snk-pesquisa__btn.sc-snk-pesquisa:disabled:hover{cursor:unset;color:var(--snk-pesquisa__btn-disabled--color)}.snk-pesquisa__btn.sc-snk-pesquisa:hover{color:var(--snk-pesquisa__btn-hover--color)}.snk-pesquisa__cards-container.sc-snk-pesquisa{height:100%;overflow-y:auto}";

const SnkPesquisa = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this._mdByName = {};
    this._startHighlightTag = "<span class='card-item__highlight'>";
    this._endHighlightTag = "</span>";
    this._specialCharsRegex = /[[.\-\$\+\*,_\&\(\)%\/\\#@!:\|\=\'\"]/gmi;
    this._charsWithAccentuation = "ÁÉÍÓÚ_ÃÕ_ÂÊÎÔÛ_ÀÈÌÒÙ_Ü_Ç_áéíóú_ãõ_âêîôû_àèìòù_ü_ç";
    this._charsWithoutAccentuation = "AEIOU_AO_AEIOU_AEIOU_U_C_aeiou_ao_aeiou_aeiou_u_c";
    this._changeDeboucingTimeout = null;
    this._limitCharsToSearch = 3;
    this._deboucingTime = 450;
    this._gridDataSource = [];
    this.pesquisaFetcher = new PesquisaFetcher();
    this._itemList = undefined;
    this._startLoading = false;
    this._presentationMode = "list";
    this._currentView = undefined;
    this._valideDataSource = false;
    this.searchLoader = undefined;
    this.treeLoader = undefined;
    this.selectItem = undefined;
    this.entityName = undefined;
    this.argument = undefined;
    this.isHierarchyEntity = false;
    this.allowsNonAnalytic = false;
  }
  observeArgument() {
    if (this._textInput) {
      this._textInput.value = this.argument;
    }
  }
  clearSearch() {
    this.argument = "";
    this._itemList = undefined;
  }
  buildDetails(fields, item) {
    let details = {};
    for (let index = 0; (index < fields.length); index++) {
      const field = fields[index];
      const itemValue = item[field.fieldName];
      const optionValue = field.options ? field.options[itemValue] || itemValue : itemValue;
      details[field.description] = this.highlightValue(item['__matchFields'], optionValue, fields, true);
    }
    return details;
  }
  buildFirstFields(matchFields, fields, qtyFields = 6) {
    if (matchFields && Array.isArray(matchFields)) {
      matchFields.forEach((matchField) => {
        let fieldMD = this.removeReference(fields, this._mdByName[matchField]);
        if (fieldMD) {
          fields.unshift(fieldMD);
        }
      });
    }
    fields = fields.slice(0, qtyFields);
    return fields;
  }
  doSearch(argument) {
    if (this.searchLoader && typeof this.searchLoader === "function") {
      this._startLoading = true;
      this.searchLoader(argument)
        .then((resp) => {
        if (!resp)
          return;
        this.processSearchResponse(resp);
      }).finally(() => {
        this._startLoading = false;
      });
    }
  }
  processSearchResponse(resp) {
    resp = ObjectUtils.stringToObject(resp.json.$);
    let descriptionField = resp.descriptionField;
    let pkField = resp.pkField;
    if (!pkField)
      return;
    this._gridMetadata = buildGridMetadata(resp);
    this._gridDataSource = [...resp.data];
    this._pkField = pkField;
    this._descriptionField = descriptionField;
    this._itemList = this.buildCardItemList(resp, pkField, descriptionField);
  }
  buildCardItemList(resp, pkField, descriptionField) {
    var _a;
    let newItemList = (_a = resp.data) === null || _a === void 0 ? void 0 : _a.map((item) => {
      var _a;
      let fieldsMetadata = this.filterFieldsMetadata(resp, item);
      let fields = this.buildFirstFields(item['__matchFields'], fieldsMetadata);
      return {
        key: this.highlightValue(item['__matchFields'], (_a = item[pkField]) === null || _a === void 0 ? void 0 : _a.toString(), fields, true),
        title: descriptionField ? this.highlightValue(item['__matchFields'], item[descriptionField], fields, true) : '',
        details: this.buildDetails(fields, item),
      };
    });
    return newItemList;
  }
  filterFieldsMetadata(resp, item) {
    return resp.fieldsMetadata.filter((field) => {
      let accept = !StringUtils.isEmpty(item[field.fieldName]) &&
        field.visible !== false &&
        field.type !== 'B' &&
        resp.pkField !== field.fieldName &&
        resp.descriptionField !== field.fieldName &&
        (field.isPrimaryKey || !field.isLinkField) &&
        !(field.type === 'S' && field.presentationType === 'H');
      if (accept) {
        this._mdByName[field.fieldName] = field;
      }
      //Condição que trata o problema de trazer imagem no entityCard na pesquisa de registros
      if (typeof item[field.fieldName] === 'string' && (item[field.fieldName].indexOf('<img') > -1 || item[field.fieldName].indexOf('<svg') > -1)) {
        return false;
      }
      return accept;
    });
  }
  removeAtIndex(array, index) {
    if (index >= 0 && index < array.length) {
      return array.splice(index, 1)[0];
    }
  }
  removeReference(array, obj) {
    let index = this.indexOf(array, obj);
    return this.removeAtIndex(array, index);
  }
  indexOf(arr, obj) {
    let index = -1;
    if (Array.isArray(arr)) {
      this.find(arr, (item, i) => {
        index = i;
        return this.equals(obj, item);
      });
    }
    return index;
  }
  equals(obj1, obj2) {
    return ObjectUtils.objectToString(obj1) === ObjectUtils.objectToString(obj2);
  }
  find(arr, checkerFn) {
    if (arr) {
      for (let i = 0, length = arr.length; i < length; i++) {
        if (checkerFn(arr[i], i, arr)) {
          return arr[i];
        }
      }
    }
    else {
      return undefined;
    }
  }
  /**
    matchFields-> array __matchFields do data
    value -> termo que está sendo pesquisao
    fieldMD -> metadados retornados pelo serviço de pesquisa
    forceMatch -> força a pesquisa mesmo que não tenha __matchFields no metadado
  */
  highlightValue(matchFields, value, fieldMD, forceMatch) {
    let valueAux = this.replaceHtmlEntities(value);
    if (this.argument && (forceMatch || this.isIn(matchFields, fieldMD.fieldName))) {
      const argumentNumber = this.getArgumentNumber();
      let replacements = (isNaN(argumentNumber) ? this.argument : argumentNumber.toString()).split(/%|,|\s+/);
      let lastMatchIndex = 0;
      let cleanText = this.replaceAccentuatedChars(valueAux);
      replacements.forEach((r) => {
        if (r) {
          r = this.replaceAccentuatedChars(r);
          let specialChars = this.getSpecialCharacters(r);
          if (specialChars != undefined && specialChars.length > 0) {
            r = this.removeSpecialCharacters(r);
          }
          if (fieldMD.mask || fieldMD.uiType === 'CGC_CPF' || fieldMD.uiType === 'Phone') {
            //Fazemos isso para descondirerar os caracteres especiais de formatação da string
            r = r.split('').join('\\.?\\-?\\/?\\(?\\)?');
          }
          let regex = new RegExp(r, "ig");
          regex.lastIndex = lastMatchIndex;
          let match = regex.exec(cleanText);
          if (match && match.length > 0) {
            lastMatchIndex = match.index;
            let sizeMatch = match[0].length;
            let txtToColor = valueAux.substring(lastMatchIndex, lastMatchIndex + sizeMatch);
            let txtColored = (txtToColor === null || txtToColor === void 0 ? void 0 : txtToColor.trim()) ? this._startHighlightTag + txtToColor + this._endHighlightTag : '';
            let start = valueAux.substring(0, lastMatchIndex);
            let end = valueAux.substring(lastMatchIndex + sizeMatch);
            valueAux = start + txtColored + end;
            lastMatchIndex = lastMatchIndex + txtColored.length;
            cleanText = this.replaceAccentuatedChars(valueAux);
          }
        }
      });
    }
    return valueAux;
  }
  replaceHtmlEntities(source) {
    if (source == undefined) {
      return source;
    }
    return String(source)
      .replace(/&/g, '&amp;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;')
      .replace(/"/g, '&quot;');
  }
  replaceAccentuatedChars(source) {
    if (source == undefined) {
      return source;
    }
    const replaceList = ['&amp;', '&lt;', '&gt;', '&quot;'];
    const sourceAux = this.replaceToSpace(source, replaceList);
    return sourceAux.replace(/[^\w ]/g, (char) => {
      let index = this._charsWithAccentuation.indexOf(char);
      if (index > -1) {
        char = this._charsWithoutAccentuation.charAt(index);
      }
      return char || '';
    });
  }
  replaceToSpace(source, replaceList = []) {
    if (source == undefined) {
      return source;
    }
    replaceList.forEach((word) => {
      const wordAux = new RegExp(word, 'g');
      let whiteSpace = '';
      for (let i = 0; i < word.length; i++) {
        whiteSpace += ' ';
      }
      source = String(source).replace(wordAux, whiteSpace);
    });
    return source;
  }
  isIn(arr, obj) {
    return this.indexOf(arr, obj) > -1;
  }
  getSpecialCharacters(str) {
    let match;
    let charList = [];
    while ((match = this._specialCharsRegex.exec(str))) {
      charList.push(str[match.index]);
    }
    return charList;
  }
  removeSpecialCharacters(str) {
    let specialChars = this.getSpecialCharacters(str);
    specialChars.forEach((ch) => {
      str = this.replaceAll(str, ch, '');
    });
    return str;
  }
  replaceAll(str, strFrom, strTo) {
    let pos = (str != undefined ? str : '').indexOf(strFrom);
    while (pos > -1) {
      str = str.replace(strFrom, strTo);
      pos = str.indexOf(strFrom);
    }
    return (str);
  }
  getArgumentNumber() {
    return Number(this.argument || undefined);
  }
  createOption(detail) {
    let { key, title } = detail;
    const startHighlight = new RegExp(this._startHighlightTag, 'g');
    const endHighlight = new RegExp(this._endHighlightTag, 'g');
    title = StringUtils.decodeHtmlEntities(title);
    const option = {
      value: key === null || key === void 0 ? void 0 : key.replace(startHighlight, '').replace(endHighlight, ''),
      label: title === null || title === void 0 ? void 0 : title.replace(startHighlight, '').replace(endHighlight, '')
    };
    this.selectItem(option);
  }
  onChangeValue(event) {
    var _a;
    this.clearDeboucingTimeout();
    if (this._startLoading) {
      this._changeDeboucingTimeout = setTimeout(() => {
        this.onChangeValue(event);
      }, this._deboucingTime);
      return;
    }
    this.argument = (event.detail || "").trim();
    if (this.isHierarchyEntity && this._presentationMode === 'list') {
      (_a = this._pesquisaTree) === null || _a === void 0 ? void 0 : _a.applyFilter();
      return;
    }
    const argumentNumber = this.getArgumentNumber();
    if (this.argument && (!isNaN(argumentNumber) || this.argument.length >= this._limitCharsToSearch)) {
      this._changeDeboucingTimeout = setTimeout(() => {
        this.doSearch(this.argument);
        this._valideDataSource = true;
      }, this._deboucingTime);
    }
    else {
      this._itemList = undefined;
      this._gridDataSource = [];
      this._valideDataSource = false;
    }
  }
  clearDeboucingTimeout() {
    if (this._changeDeboucingTimeout) {
      clearTimeout(this._changeDeboucingTimeout);
      this._changeDeboucingTimeout = undefined;
    }
  }
  onClickSearch() {
    this.doSearch((this.argument || "").trim());
    this._valideDataSource = true;
  }
  getMessageView() {
    if (this._startLoading)
      return this.getMessage('snkPesquisa.search.waitMessage');
    if (!this._itemList)
      return this.getMessage('snkPesquisa.search.initialMessage');
    const itemsCount = this._itemList.length;
    if (itemsCount < 1)
      return this.getMessage('snkPesquisa.search.noResultsMessage');
    if (itemsCount === 1)
      return this.getMessage('snkPesquisa.search.resultsFoundSingular');
    return this.getMessage('snkPesquisa.search.resultsFoundPlural', { COUNT: itemsCount });
  }
  async componentWillLoad() {
    var _a;
    this._application = ApplicationContext.getContextValue("__SNK__APPLICATION__");
    this._messagesBuilder = (_a = this._application) === null || _a === void 0 ? void 0 : _a.messagesBuilder;
    const mode = await this.pesquisaFetcher.loadPresentationConfig(this.entityName);
    this._presentationMode = mode === 'list' ? 'list' : 'table';
  }
  async componentDidLoad() {
    ElementIDUtils.addIDInfo(this._element);
    await this.chooseViewMode();
  }
  getMessage(key, params) {
    return this._messagesBuilder.getMessage(key, params);
  }
  async handleChangeViewMode() {
    this._presentationMode = this._presentationMode === 'list' ? 'table' : 'list';
    await this.chooseViewMode();
    await this.pesquisaFetcher.savePresentationConfig(this.entityName, this._presentationMode);
  }
  async chooseViewMode() {
    var _a, _b;
    if (this._presentationMode !== 'list') {
      await ((_a = this._viewStack) === null || _a === void 0 ? void 0 : _a.show(PESQUISA_VIEW_MODE.GRID));
      this._currentView = PESQUISA_VIEW_MODE.GRID;
      return;
    }
    const view = this.isHierarchyEntity ? PESQUISA_VIEW_MODE.TREE : PESQUISA_VIEW_MODE.CARDS;
    await ((_b = this._viewStack) === null || _b === void 0 ? void 0 : _b.show(view));
    this._currentView = view;
  }
  //Os limites de exibição são diferentes para lista e grade
  hideLimitMessage() {
    if (!this._itemList)
      return true;
    return this._itemList.length < 100;
  }
  hideRecordsMessage() {
    return (this._presentationMode === "list" && this.isHierarchyEntity);
  }
  getButtonModeInfo() {
    if (this._presentationMode === 'list') {
      return {
        iconName: 'table',
        title: this.getMessage('snkPesquisa.viewMode.tableMode'),
      };
    }
    if (this.isHierarchyEntity) {
      return {
        iconName: 'hierarchical-tree',
        title: this.getMessage('snkPesquisa.viewMode.tableMode'),
      };
    }
    return {
      iconName: 'list',
      title: this.getMessage('snkPesquisa.viewMode.listMode'),
    };
  }
  render() {
    var _a, _b;
    return (h(Host, null, h("div", { class: "snk-pesquisa" }, h("div", { class: "snk-pesquisa__input" }, h("ez-text-input", { label: this.getMessage('snkPesquisa.search.label'), class: "ez-margin-right--medium", canShowError: false, ref: (ref) => this._textInput = ref, onEzChange: (event) => this.onChangeValue(event), value: this.argument, "data-element-id": "searchInput" }, h("ez-icon", { slot: "leftIcon", iconName: "search" }), this.argument && ((_a = this._textInput) === null || _a === void 0 ? void 0 : _a.value)
      ? h("button", { slot: "rightIcon", class: "snk-pesquisa__btn", onClick: () => this.clearSearch() }, h("ez-icon", { iconName: "close" }))
      : undefined), h("ez-button", { class: "ez-button--primary", label: this.getMessage('snkPesquisa.search.button'), onClick: () => this.onClickSearch() }), h("ez-button", { class: "ez-margin-left--medium", mode: "icon", iconName: this.getButtonModeInfo().iconName, title: this.getButtonModeInfo().title, onClick: async () => await this.handleChangeViewMode() })), h("div", { class: `snk-pesquisa__records-label ${this._currentView === PESQUISA_VIEW_MODE.TREE ? 'hidden' : ''}` }, h("label", { class: `snk-pesquisa__records ${this.hideRecordsMessage() ? "hidden" : ""}` }, this.getMessageView()), h("label", { class: `snk-pesquisa__records-limit ${this.hideLimitMessage() ? "hidden" : ""}` }, this.getMessage('snkPesquisa.search.limitMessage'))), h("div", { class: "snk-pesquisa__content" }, h("ez-view-stack", { ref: (ref) => this._viewStack = ref, "data-element-id": "simple-crud" }, h("stack-item", null, this._valideDataSource &&
      h("pesquisa-grid", { metadata: this._gridMetadata, dataSource: this._gridDataSource, pkField: this._pkField, descriptionField: this._descriptionField, entityName: this.entityName, shouldLoadConfig: this._currentView === PESQUISA_VIEW_MODE.GRID, onPesquisaGridSelectItem: ({ detail }) => this.selectItem(detail) })), h("stack-item", null, h("div", { class: 'snk-pesquisa__cards-container' }, (_b = this._itemList) === null || _b === void 0 ? void 0 : _b.map((item) => {
      return (h("ez-card-item", { onEzClick: (event) => this.createOption(event.detail), item: item }));
    }))), h("stack-item", null, h("pesquisa-tree", { ref: (element) => this._pesquisaTree = element, argument: this.argument, treeLoader: this.treeLoader, allowsNonAnalytic: this.allowsNonAnalytic, shouldLoadTree: this._currentView === PESQUISA_VIEW_MODE.TREE, onPesquisaTreeSelectItem: ({ detail }) => this.selectItem(detail), messagesBuilder: this._messagesBuilder })))))));
  }
  get _element() { return getElement(this); }
  static get watchers() { return {
    "argument": ["observeArgument"]
  }; }
};
SnkPesquisa.style = snkPesquisaCss;

export { SnkPesquisa as snk_pesquisa };
