import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
import { I as InMemoryLoader } from './dataunit-fetcher.js';
import './DataFetcher.js';
import { a as PesquisaFetcher } from './ISave.js';
import '@sankhyalabs/core';
import '@sankhyalabs/ezui/dist/collection/utils/constants';
import '@sankhyalabs/core/dist/dataunit/metadata/UnitMetadata';
import './filter-item-type.enum.js';
import './form-config-fetcher.js';
import './constants.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 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    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"]
  }; }
  static get style() { return pesquisaGridCss; }
}, [2, "pesquisa-grid", {
    "metadata": [16],
    "dataSource": [16],
    "pkField": [1, "pk-field"],
    "descriptionField": [1, "description-field"],
    "entityName": [1, "entity-name"],
    "shouldLoadConfig": [4, "should-load-config"],
    "_gridConfig": [32],
    "_inMemoryLoader": [32],
    "_dataunit": [32]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["pesquisa-grid"];
  components.forEach(tagName => { switch (tagName) {
    case "pesquisa-grid":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, PesquisaGrid);
      }
      break;
  } });
}

export { PesquisaGrid as P, defineCustomElement as d };
