'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-17105557.js');
const dataunitFetcher = require('./dataunit-fetcher-6c85a04d.js');
require('./DataFetcher-21f8c2dc.js');
const ISave = require('./ISave-f926fe9e.js');
require('@sankhyalabs/core');
require('./index-e3e39724.js');
require('@sankhyalabs/ezui/dist/collection/utils/constants');
require('@sankhyalabs/core/dist/dataunit/metadata/UnitMetadata');
require('./filter-item-type.enum-a7ffdaa6.js');
require('./form-config-fetcher-fef79845.js');
require('./constants-6d82e405.js');
const utils = require('@sankhyalabs/ezui/dist/collection/utils');
require('@sankhyalabs/ezui/dist/collection/components/ez-grid/utils');
require('@sankhyalabs/core/dist/utils/SortingUtils');
require('./ResourceIDUtils-5ff86aa7.js');
require('./PrintUtils-bcaeb82f.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) {
    index.registerInstance(this, hostRef);
    this.pesquisaGridSelectItem = index.createEvent(this, "pesquisaGridSelectItem", 7);
    this._pesquisaFetcher = new ISave.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 dataunitFetcher.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 (index.h(index.Host, null, index.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) {
    index.registerInstance(this, hostRef);
    this.pesquisaTreeSelectItem = index.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');
      utils.ApplicationUtils.error(title, message);
      return;
    }
    const option = {
      value: item.id,
      label: item.label,
    };
    this.pesquisaTreeSelectItem.emit(option);
  }
  renderEmptyMessage() {
    return (index.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" }, index.h("span", null, this.getMessage('snkPesquisa.tree.emptyMessage'))));
  }
  getMessage(key, params) {
    return this.messagesBuilder.getMessage(key, params);
  }
  render() {
    var _a;
    return (index.h(index.Host, null, !((_a = this.items) === null || _a === void 0 ? void 0 : _a.length) ?
      this.renderEmptyMessage() :
      index.h(index.Fragment, null, index.h("div", { class: 'actions-container' }, index.h("ez-button", { size: 'small', mode: 'icon', class: 'ez-margin-right--small', iconName: 'show-list', title: this.getMessage('snkPesquisa.tree.expandButton'), onClick: () => this.handleExpandAll() }), index.h("ez-button", { size: 'small', mode: 'icon', iconName: 'hide-list', title: this.getMessage('snkPesquisa.tree.collapseButton'), onClick: () => this.handleCloseAll() })), index.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;

exports.pesquisa_grid = PesquisaGrid;
exports.pesquisa_tree = PesquisaTree;
