import { r as registerInstance, h } from './index-baa5e267.js';
import { RECORD_DATE_FORMAT, DataUnitInMemoryLoader } from '@sankhyalabs/core';

const ezGridViewCss = "";

const EzGridView = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.inMemoryLoader = undefined;
    this.metadata = undefined;
    this.records = undefined;
    this.columnsConfig = undefined;
    this.pageSize = 150;
    this.recordDateFormat = RECORD_DATE_FORMAT.DD_MM_YYYY;
    this.multipleSelection = false;
    this.autoFocus = true;
    this.paginationCounterMode = 'auto';
    this.suppressCheckboxColumn = false;
    this.suppressFilterColumn = false;
    this.outlineMode = false;
    this.enableRowTableStriped = true;
    this.compact = false;
    this.useSearchColumn = true;
    this.suppressHorizontalScroll = false;
  }
  /**
   * Obtém o DataUnit da grade.
   */
  async getDataUnit() {
    if (!this.hasValidDependencies())
      return;
    this.ensureInMemoryLoaderExists();
    return this.inMemoryLoader.dataUnit;
  }
  /**
   * Recarrega os registros da grade.
   */
  async refresh() {
    await this.inMemoryLoader.dataUnit.loadData();
  }
  /**
   * Obtém as linhas selecionadas.
   */
  async getSelection() {
    var _a;
    return (_a = this._gridRef) === null || _a === void 0 ? void 0 : _a.getSelection();
  }
  /**
   * Aplica um filtro rápido.
   */
  async quickFilter(term) {
    var _a;
    await ((_a = this._gridRef) === null || _a === void 0 ? void 0 : _a.quickFilter(term));
  }
  /**
   * Localiza determinada coluna tornando-a visível.
   */
  async locateColumn(columnName) {
    var _a;
    await ((_a = this._gridRef) === null || _a === void 0 ? void 0 : _a.locateColumn(columnName));
  }
  /**
   * Usa um argumento para filtrar as colunas por label
   */
  async filterColumns(search) {
    var _a;
    return (_a = this._gridRef) === null || _a === void 0 ? void 0 : _a.filterColumns(search);
  }
  /**
   * Adiciona item de menu nas colunas.
   */
  async addColumnMenuItem(label, name, action, icon) {
    await this._gridRef.addColumnMenuItem(label, name, action, icon);
  }
  /**
   * Registra um formatador de valores para uma coluna da grid.
   */
  async addCustomValueFormatter(columnName, customFormatter) {
    var _a;
    await ((_a = this._gridRef) === null || _a === void 0 ? void 0 : _a.addCustomValueFormatter(columnName, customFormatter));
  }
  /**
   * Remove o formatador de valores de uma coluna da grid.
   */
  async removeCustomValueFormatter(columnName) {
    var _a;
    await ((_a = this._gridRef) === null || _a === void 0 ? void 0 : _a.removeCustomValueFormatter(columnName));
  }
  /**
   * Registra um render customizado para colunas da grid.
   */
  async addGridCustomRender(fieldName, customRender, detailContext) {
    var _a;
    await ((_a = this._gridRef) === null || _a === void 0 ? void 0 : _a.addGridCustomRender(fieldName, customRender, detailContext));
  }
  /**
   * Atribui o foco para a grade.
   */
  async setFocus() {
    var _a;
    await ((_a = this._gridRef) === null || _a === void 0 ? void 0 : _a.setFocus());
  }
  async handleMetadataChanged() {
    await this.configGrid();
  }
  async handleRecordsChanged() {
    await this.configGrid();
  }
  async handlePageSizeChanged() {
    await this.configGrid(true);
  }
  async handleRecordDateFormatChanged() {
    await this.configGrid(true);
  }
  async configGrid(forceRebuild = false) {
    if (!this.hasValidDependencies())
      return;
    this.ensureInMemoryLoaderExists(forceRebuild);
    this.updateDURecords();
    this.refreshFilterColumnDataSource();
  }
  updateDURecords() {
    requestAnimationFrame(() => {
      this.inMemoryLoader.records = this.records;
    });
  }
  refreshFilterColumnDataSource() {
    requestAnimationFrame(async () => {
      var _a;
      await ((_a = this._gridRef) === null || _a === void 0 ? void 0 : _a.refreshColumnFilterDataSource());
    });
  }
  hasValidDependencies() {
    return this.metadata && this.records;
  }
  buildGridConfig() {
    if (!this.columnsConfig)
      return;
    const columns = this.columnsConfig.map(this.parseColumnConfig.bind(this));
    return { columns };
  }
  parseColumnConfig(conf, index) {
    return Object.assign(Object.assign({}, conf), { orderIndex: index });
  }
  ensureInMemoryLoaderExists(forceRebuild = false) {
    if (forceRebuild) {
      this.clearInMemoryLoader();
    }
    this.buildInMemoryLoader();
  }
  buildInMemoryLoader() {
    requestAnimationFrame(() => {
      if (!this.inMemoryLoader) {
        this.inMemoryLoader = new DataUnitInMemoryLoader(this.metadata, this.records, this.buildInMemoryLoaderConfig());
      }
    });
  }
  clearInMemoryLoader() {
    this.inMemoryLoader = undefined;
  }
  buildInMemoryLoaderConfig() {
    const config = {
      pageSize: this.pageSize,
      recordDateFormat: this.recordDateFormat,
    };
    return config;
  }
  async componentDidLoad() {
    await this.configGrid();
  }
  render() {
    var _a;
    if (!((_a = this.inMemoryLoader) === null || _a === void 0 ? void 0 : _a.dataUnit) || !this.records)
      return;
    return (h("ez-grid", { ref: ref => this._gridRef = ref, canEdit: false, dataUnit: this.inMemoryLoader.dataUnit, config: this.buildGridConfig(), multipleSelection: this.multipleSelection, autoFocus: this.autoFocus, paginationCounterMode: this.paginationCounterMode, suppressCheckboxColumn: this.suppressCheckboxColumn, suppressFilterColumn: this.suppressFilterColumn, suppressHorizontalScroll: this.suppressHorizontalScroll, outlineMode: this.outlineMode, enableRowTableStriped: this.enableRowTableStriped, useSearchColumn: this.useSearchColumn, compact: this.compact, onEzPageChangedChanged: () => this.refreshFilterColumnDataSource() }, h("slot", { name: "leftButtons" }), h("slot", { name: "footer" })));
  }
  static get watchers() { return {
    "metadata": ["handleMetadataChanged"],
    "records": ["handleRecordsChanged"],
    "pageSize": ["handlePageSizeChanged"],
    "recordDateFormat": ["handleRecordDateFormatChanged"]
  }; }
};
EzGridView.style = ezGridViewCss;

export { EzGridView as ez_grid_view };
