import { r as registerInstance, e as createEvent, h, f as Host, F as Fragment } from './index-e9c7ceaa.js';
import { I as InMemoryLoader } from './dataunit-fetcher-6cacb959.js';
import './DataFetcher-955ee53e.js';
import { P as PesquisaFetcher } from './ISave-c8bf12c4.js';
import '@sankhyalabs/core';
import './index-b40568ff.js';
import '@sankhyalabs/ezui/dist/collection/utils/constants';
import '@sankhyalabs/core/dist/dataunit/metadata/UnitMetadata';
import './filter-item-type.enum-d45e026f.js';
import './form-config-fetcher-baa89098.js';
import './constants-7b422de0.js';
import { ApplicationUtils } from '@sankhyalabs/ezui/dist/collection/utils';
import '@sankhyalabs/ezui/dist/collection/components/ez-grid/utils';
import '@sankhyalabs/core/dist/utils/SortingUtils';
import './ResourceIDUtils-a114189a.js';
import './PrintUtils-3e4ff0f5.js';

const pesquisaGridCss = ".sc-pesquisa-grid-h{background:var(--background--xlight, #FFF);overflow-x:auto}ez-grid.sc-pesquisa-grid{--ez-grid__container--shadow:none;--ez-grid__header--shadow:none}";

const PesquisaGrid = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.pesquisaGridSelectItem = createEvent(this, "pesquisaGridSelectItem", 7);
    this._pesquisaFetcher = new PesquisaFetcher();
    this.metadata = undefined;
    this.dataSource = [];
    this.pkField = undefined;
    this.descriptionField = undefined;
    this.entityName = undefined;
    this.shouldLoadConfig = false;
    this._gridConfig = undefined;
    this._inMemoryLoader = undefined;
    this._dataunit = undefined;
  }
  observeDataSource(newValue) {
    if (!this._inMemoryLoader || !this._dataunit || !newValue)
      return;
    this._inMemoryLoader.records = [...newValue];
  }
  observeMetadata() {
    this.initializeDataUnit();
  }
  async observeShouldLoadConfig(newValue) {
    if (!newValue || this._gridConfig)
      return;
    await this.loadGridConfig();
  }
  async loadGridConfig() {
    this._gridConfig = await this._pesquisaFetcher.loadPesquisaGridConfig(this.entityName);
  }
  initializeDataUnit() {
    var _a;
    if (!this.metadata || this._dataunit)
      return;
    this._inMemoryLoader = new InMemoryLoader(this.metadata, (_a = this.dataSource) !== null && _a !== void 0 ? _a : []);
    this._dataunit = this._inMemoryLoader.dataUnit;
    this._dataunit.pageSize = 0;
  }
  handleDbClick(record) {
    const option = {
      value: record[this.pkField],
      label: record[this.descriptionField],
    };
    this.pesquisaGridSelectItem.emit(option);
  }
  async handleConfigChanged({ detail }) {
    var _a;
    const columns = (_a = detail === null || detail === void 0 ? void 0 : detail.columns) !== null && _a !== void 0 ? _a : [];
    await this._pesquisaFetcher.savePesquisaGridConfig(this.entityName, columns);
  }
  render() {
    if (!this.metadata || !this._dataunit)
      return;
    return (h(Host, null, h("ez-grid", { dataUnit: this._dataunit, config: this._gridConfig, mode: "simple", autoFocus: false, onEzDoubleClick: ({ detail }) => this.handleDbClick(detail), onConfigChange: async (event) => this.handleConfigChanged(event) })));
  }
  static get watchers() { return {
    "dataSource": ["observeDataSource"],
    "metadata": ["observeMetadata"],
    "shouldLoadConfig": ["observeShouldLoadConfig"]
  }; }
};
PesquisaGrid.style = pesquisaGridCss;

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 = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    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"]
  }; }
};
PesquisaTree.style = pesquisaTreeCss;

export { PesquisaGrid as pesquisa_grid, PesquisaTree as pesquisa_tree };
