import { proxyCustomElement, HTMLElement, createEvent, h, Host, Fragment } from '@stencil/core/internal/client';
import { ApplicationUtils } from '@sankhyalabs/ezui/dist/collection/utils';

const pesquisaTreeCss = ".sc-pesquisa-tree-h{--snk-pesquisa--tree__action-container--top:var(--space--sm);--snk-pesquisa--tree__action-container--right:var(--space--small);--snk-pesquisa--tree__min-height:var(--space--extra-large);background:var(--background--xlight, #FFF);position:relative;min-height:var(--snk-pesquisa--tree__min-height);height:100%;display:block;overflow-y:auto}.sc-pesquisa-tree-h:hover .actions-container.sc-pesquisa-tree{display:flex}.actions-container.sc-pesquisa-tree{flex-direction:row;position:absolute;top:var(--snk-pesquisa--tree__action-container--top);right:var(--snk-pesquisa--tree__action-container--right);display:none}";

const PesquisaTree = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.pesquisaTreeSelectItem = createEvent(this, "pesquisaTreeSelectItem", 7);
    this.treeLoader = undefined;
    this.argument = undefined;
    this.allowsNonAnalytic = false;
    this.shouldLoadTree = false;
    this.messagesBuilder = undefined;
    this.items = [];
  }
  async applyFilter() {
    await this._tree.applyFilter(this.argument);
  }
  async observeShouldLoadTree(newValue) {
    if (!newValue || this.items.length > 0)
      return;
    await this.loadTree();
  }
  async loadTree() {
    if (!this.shouldLoadTree)
      return;
    if (!this.treeLoader || typeof this.treeLoader !== 'function')
      return;
    const response = await this.treeLoader((this.argument || '').trim());
    this.items = [...response];
  }
  expandOrCloseAllItems(items, expanded) {
    return items.map(item => (Object.assign(Object.assign({}, item), { expanded, children: item.children ? this.expandOrCloseAllItems(item.children, expanded) : undefined })));
  }
  handleExpandAll() {
    this.items = [...this.expandOrCloseAllItems(this.items, true)];
  }
  handleCloseAll() {
    this.items = [...this.expandOrCloseAllItems(this.items, false)];
  }
  handleDbClick(item) {
    var _a;
    if (!this.allowsNonAnalytic && !((_a = item['props']) === null || _a === void 0 ? void 0 : _a['analitico'])) {
      const title = this.getMessage('snkPesquisa.tree.errorTitle');
      const message = this.getMessage('snkPesquisa.tree.nonAnalyticError');
      ApplicationUtils.error(title, message);
      return;
    }
    const option = {
      value: item.id,
      label: item.label,
    };
    this.pesquisaTreeSelectItem.emit(option);
  }
  renderEmptyMessage() {
    return (h("div", { class: "ez-flex ez-flex--justify-center ez-flex--align-items-center ez-margin--large ez-text ez-text--xlarge ez-text--secondary" }, h("span", null, this.getMessage('snkPesquisa.tree.emptyMessage'))));
  }
  getMessage(key, params) {
    return this.messagesBuilder.getMessage(key, params);
  }
  render() {
    var _a;
    return (h(Host, null, !((_a = this.items) === null || _a === void 0 ? void 0 : _a.length) ?
      this.renderEmptyMessage() :
      h(Fragment, null, h("div", { class: 'actions-container' }, h("ez-button", { size: 'small', mode: 'icon', class: 'ez-margin-right--small', iconName: 'show-list', title: this.getMessage('snkPesquisa.tree.expandButton'), onClick: () => this.handleExpandAll() }), h("ez-button", { size: 'small', mode: 'icon', iconName: 'hide-list', title: this.getMessage('snkPesquisa.tree.collapseButton'), onClick: () => this.handleCloseAll() })), h("ez-tree", { ref: (element) => this._tree = element, onEzDbClickItem: ({ detail }) => this.handleDbClick(detail), items: this.items }))));
  }
  static get watchers() { return {
    "shouldLoadTree": ["observeShouldLoadTree"]
  }; }
  static get style() { return pesquisaTreeCss; }
}, [2, "pesquisa-tree", {
    "treeLoader": [16],
    "argument": [1025],
    "allowsNonAnalytic": [4, "allows-non-analytic"],
    "shouldLoadTree": [4, "should-load-tree"],
    "messagesBuilder": [1040],
    "items": [32],
    "applyFilter": [64]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["pesquisa-tree"];
  components.forEach(tagName => { switch (tagName) {
    case "pesquisa-tree":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, PesquisaTree);
      }
      break;
  } });
}

export { PesquisaTree as P, defineCustomElement as d };
