import { h, proxyCustomElement, HTMLElement, createEvent } from '@stencil/core/internal/client';
import { UserInterface, JSUtils, ElementIDUtils, ApplicationContext } from '@sankhyalabs/core';

var SelectedItemType;
(function (SelectedItemType) {
  SelectedItemType["LINK"] = "LINK";
  SelectedItemType["FIELD"] = "FIELD";
})(SelectedItemType || (SelectedItemType = {}));

const ICON_MODES = {
  [UserInterface.SHORTTEXT]: 'text',
  [UserInterface.LONGTEXT]: 'text',
  [UserInterface.INTEGERNUMBER]: 'number',
  [UserInterface.DECIMALNUMBER]: 'number',
  [UserInterface.DATE]: 'calendar',
  [UserInterface.DATETIME]: 'calendar',
  [UserInterface.SWITCH]: 'boolean',
  [UserInterface.CHECKBOX]: 'check-circle-inverted',
  [UserInterface.OPTIONSELECTOR]: 'list',
  [UserInterface.SEARCH]: 'search',
  [UserInterface.HTML]: 'tag_code',
  [UserInterface.IMAGE]: 'png',
  [UserInterface.FILE]: 'edit-file',
  [UserInterface.TIME]: 'timer-outline',
  [UserInterface.ELAPSEDTIME]: 'timer',
  [UserInterface.MASKEDTEXT]: 'text',
  [UserInterface.PASSWORD]: 'email',
};
const ListItem = ({ userInterface, description }) => {
  return (h("ez-icon", { iconName: ICON_MODES[userInterface], class: "ez-padding-left--small", title: description }));
};

const snkFilterFieldSearchCss = ".sc-snk-filter-field-search-h{--snk-filter-field-search--container-width:468px;--snk-filter-field-search--container-height:322px}.snk-filter-field-search__container.sc-snk-filter-field-search{display:flex;flex-direction:column;padding:var(--space--medium);width:var(--snk-filter-field-search--container-width);height:var(--snk-filter-field-search--container-height)}.snk-filter-field-search__section.sc-snk-filter-field-search{display:grid;grid-template-columns:1fr 1fr;place-items:flex-start;overflow:hidden;gap:var(--space--small)}.snk-filter-field-search__input.sc-snk-filter-field-search{margin-bottom:var(--space--medium)}.snk-filter-field-search__list_container.sc-snk-filter-field-search{display:flex;flex-direction:column}.snk-filter-field-search__list.sc-snk-filter-field-search{overflow-y:auto}";

const SNK_CONTEXT_KEY = "__SNK__APPLICATION__";
const ENTITY_NAME_PATTERN = /\w+:\/\/(\w+)/;
const SnkFilterFieldSearch = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.ezSelectFilterItem = createEvent(this, "ezSelectFilterItem", 7);
    this._currentMetadata = null;
    this._filterText = "";
    this._isLoading = false;
    this.searchable = true;
    this.fieldsDataSource = undefined;
    this.breadcrumbItems = [];
    this.linkItems = [];
    this.fieldItems = [];
    this.searchEmpty = false;
    this.groupEmpty = false;
  }
  /**
   * /**
   * Realiza a abertura do componente abaixo do elemento HTML informado
   * e faz a primeira carga de dados.
   *
   * @param {HTMLElement} element - Elemento HTML onde o snk-filter-field-search irá se posicionar abaixo.
   * @param {IEzPopoverAnchorOptions} options - opções para regular o posicionamento do componente.
   */
  async show(element, options) {
    var _a;
    if (element) {
      (_a = this._ezPopover) === null || _a === void 0 ? void 0 : _a.showUnder(element, options);
    }
    else {
      this._ezPopover.show();
    }
    return this.loadData();
  }
  /**
   * Filtra a fonte de dados do componente.
   * @param {string} filterText - Texto para filtrar a fonte de dados.
   * @throws {Error} - Quando o componente estiver com a propriedade searchable como true.
  */
  async applyFilter(filterText) {
    if (this.searchable)
      throw new Error('This method is not available when searchable is true');
    this.handleFilterChange(filterText);
  }
  async loadData() {
    var _a, _b;
    this._isLoading = true;
    let metadata = await this.fieldsDataSource.fetchData(this._currentLink);
    this._currentLink = metadata.currentLink;
    const { fields, links } = metadata || {};
    if (!Array.isArray(fields) || !Array.isArray(links))
      throw new Error('Invalid metadata');
    this._currentMetadata = metadata;
    this.mapDataSourceToLinkItems();
    this.mapDataSourceToFieldItems();
    this._isLoading = false;
    (_a = this._ezFieldList) === null || _a === void 0 ? void 0 : _a.scrollToTop();
    (_b = this._ezLinkList) === null || _b === void 0 ? void 0 : _b.scrollToTop();
  }
  mapDataSourceToLinkItems() {
    var _a;
    const { links } = this._currentMetadata;
    const filterLinkLabelMessage = this.getMessage('snkFilterFieldSearch.linkLabel', { link: (_a = this._currentLink) === null || _a === void 0 ? void 0 : _a.description });
    const items = links.map(link => (Object.assign({ label: link.description }, link)));
    this.groupEmpty = Boolean(items.length === 0);
    this.linkItems = [{
        group: filterLinkLabelMessage,
        items,
      }];
    if (this.breadcrumbItems.length === 0) {
      this.breadcrumbItems = [Object.assign({ id: JSUtils.generateUUID(), label: this._currentLink.description }, this._currentLink)];
    }
  }
  mapDataSourceToFieldItems() {
    var _a;
    const fields = this._currentMetadata.fields;
    const filterFieldLabelMessage = this.getMessage('snkFilterFieldSearch.fieldLabel', { link: (_a = this._currentLink) === null || _a === void 0 ? void 0 : _a.description });
    const filteredFields = fields.filter(field => {
      if (!this._currentLink)
        return true;
      const searchableByDescription = field.description.toLowerCase().includes(this._filterText.toLowerCase());
      const searchableByName = field.name.toLowerCase().includes(this._filterText.toLowerCase());
      return searchableByDescription || searchableByName;
    });
    const items = filteredFields.map(field => (Object.assign({ label: field.description }, field)));
    this.searchEmpty = Boolean(this._filterText && items.length === 0);
    this.fieldItems = [{
        group: filterFieldLabelMessage,
        items
      }];
  }
  handleFilterChange(filterText) {
    if (this._isLoading)
      return;
    this._filterText = filterText;
    this.mapDataSourceToFieldItems();
  }
  handleSelectLink({ detail: item }) {
    if (this._isLoading)
      return;
    const breadcrumbByIndex = this.breadcrumbItems.findIndex(breadcrumbItem => breadcrumbItem.id === item.id);
    if (breadcrumbByIndex > -1) {
      this.breadcrumbItems = this.breadcrumbItems.slice(0, (breadcrumbByIndex + 1));
    }
    else {
      this.breadcrumbItems = [...this.breadcrumbItems, Object.assign({ id: JSUtils.generateUUID(), label: item.description }, item)];
    }
    const selectedItem = Object.assign(Object.assign({}, item), { type: SelectedItemType.LINK });
    this._currentLink = selectedItem;
    this.ezSelectFilterItem.emit(selectedItem);
    this.loadData();
  }
  getEntityFromBreadCrumbItem(item) {
    const result = ENTITY_NAME_PATTERN.exec(item.uri);
    if (!result) {
      return undefined;
    }
    return result[1];
  }
  handleSelectField({ detail: item }) {
    var _a;
    const breadcrumbPath = this.breadcrumbItems.map(breadcrumbItem => breadcrumbItem.label).join('>>');
    const selectedField = Object.assign(Object.assign({}, item), { type: SelectedItemType.FIELD, entityPath: this.breadcrumbItems.map(item => this.getEntityFromBreadCrumbItem(item)).filter(item => item != undefined), path: `${breadcrumbPath} >> ${item.description}` });
    this.ezSelectFilterItem.emit(selectedField);
    (_a = this._ezPopover) === null || _a === void 0 ? void 0 : _a.hide();
  }
  getElementID(sufix) {
    return {
      [ElementIDUtils.DATA_ELEMENT_ID_ATTRIBUTE_NAME]: ElementIDUtils.getInternalIDInfo(sufix)
    };
  }
  /**
   * Conforme mecanismo de mensagens, é possível customizar as mensagens dos blocos de construção
   * através de um pequeno modulo na estrutura da aplicação:
   * - Criar um arquivo no seguinte caminho: /messages/appmessages.msg.js.
   * Para conhecer os detalhes do módulo, vide o arquivo neste projeto "/src/lib/message/resources/snk-filter-field-search.msg.ts"
  */
  getMessage(key, params) {
    var _a, _b, _c;
    if (!this._application)
      this._application = ApplicationContext.getContextValue(SNK_CONTEXT_KEY);
    return (_c = (_b = (_a = this._application) === null || _a === void 0 ? void 0 : _a.messagesBuilder) === null || _b === void 0 ? void 0 : _b.getMessage) === null || _c === void 0 ? void 0 : _c.call(_b, key, params);
  }
  ;
  componentWillLoad() {
    this._application = ApplicationContext.getContextValue(SNK_CONTEXT_KEY);
  }
  componentDidLoad() {
    ElementIDUtils.addIDInfoIfNotExists(this._ezPopover, 'popover');
  }
  disconnectedCallback() {
    var _a;
    (_a = this._ezPopover) === null || _a === void 0 ? void 0 : _a.hide();
  }
  render() {
    var _a;
    const searchLabelMessage = this.getMessage('snkFilterFieldSearch.searchLabel');
    return (h("ez-popover", Object.assign({ ref: (el) => this._ezPopover = el, role: "dialog", overlayType: "none", "aria-hidden": !((_a = this._ezPopover) === null || _a === void 0 ? void 0 : _a.opened) }, this.getElementID('EzPopover')), h("div", { class: "snk-filter-field-search__container" }, h("header", { class: "snk-filter-field-search__header" }, this.searchable && !!searchLabelMessage && (h("ez-filter-input", Object.assign({ class: "snk-filter-field-search__input", label: searchLabelMessage, asyncSearch: true, canShowError: false, onEzSearching: (event) => this.handleFilterChange.bind(this)(event.detail), "aria-label": searchLabelMessage, "aria-required": "false", "aria-invalid": "false" }, this.getElementID('EzFilterInput')))), this.breadcrumbItems.length > 0 && (h("ez-breadcrumb", Object.assign({ items: this.breadcrumbItems, onSelectedItem: this.handleSelectLink.bind(this), role: "navigation", "aria-current": "step" }, this.getElementID('EzBreadcrumb')))), h("hr", { class: "ez-divider-horizontal ez-margin-vertical--medium" })), h("section", { class: `snk-filter-field-search__section ${(this.groupEmpty && this.searchEmpty) && " ez-margin--auto"}` }, this.groupEmpty ?
      h("div", { class: "ez-margin--auto" }, h("span", { class: "ez-text ez-text--secondary ez-text--medium" }, this.getMessage('snkFilterFieldSearch.groupEmpty')))
      :
        h("ez-list", Object.assign({ ref: (el) => this._ezLinkList = el, class: "snk-filter-field-search__list", id: "filterLinkList", useGroups: true, dataSource: this.linkItems, onEzSelectItem: this.handleSelectLink.bind(this), hoverFeedback: true, ezSelectable: true, itemSlotBuilder: listItem => h("ez-badge", { label: listItem.fieldCount }), "aria-describedby": "filterLinkLabel" }, this.getElementID('EzListLinks'))), this.searchEmpty ?
      h("div", { class: "ez-margin--auto" }, h("span", { class: "ez-text ez-text--secondary ez-text--medium" }, this.getMessage('snkFilterFieldSearch.searchEmpty')))
      :
        h("ez-list", Object.assign({ ref: (el) => this._ezFieldList = el, class: "snk-filter-field-search__list", id: "filterFieldList", useGroups: true, dataSource: this.fieldItems, onEzSelectItem: this.handleSelectField.bind(this), hoverFeedback: true, ezSelectable: true, itemSlotBuilder: ListItem, "aria-describedby": "filterFieldLabel" }, this.getElementID('EzListFields')))))));
  }
  static get style() { return snkFilterFieldSearchCss; }
}, [2, "snk-filter-field-search", {
    "searchable": [4],
    "fieldsDataSource": [16],
    "breadcrumbItems": [32],
    "linkItems": [32],
    "fieldItems": [32],
    "searchEmpty": [32],
    "groupEmpty": [32],
    "show": [64],
    "applyFilter": [64]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["snk-filter-field-search"];
  components.forEach(tagName => { switch (tagName) {
    case "snk-filter-field-search":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, SnkFilterFieldSearch);
      }
      break;
  } });
}

export { SnkFilterFieldSearch as S, SelectedItemType as a, defineCustomElement as d };
