import { h, Host } from '@stencil/core';
import InMemoryLoader from '../../../lib/dataUnit/InMemoryLoader';
import { PesquisaFetcher } from '../../../lib';
export class PesquisaGrid {
  constructor() {
    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 is() { return "pesquisa-grid"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() {
    return {
      "$": ["pesquisa-grid.css"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["pesquisa-grid.css"]
    };
  }
  static get properties() {
    return {
      "metadata": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "UnitMetadata",
          "resolved": "UnitMetadata",
          "references": {
            "UnitMetadata": {
              "location": "import",
              "path": "@sankhyalabs/core"
            }
          }
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Campos que representam as colunas da grade."
        }
      },
      "dataSource": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "Record[]",
          "resolved": "Record[]",
          "references": {
            "Record": {
              "location": "import",
              "path": "@sankhyalabs/core"
            }
          }
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Registros da grade"
        },
        "defaultValue": "[]"
      },
      "pkField": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Campo que representa a chave prim\u00E1ria do registro"
        },
        "attribute": "pk-field",
        "reflect": false
      },
      "descriptionField": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Campo que representa a descri\u00E7\u00E3o do registro"
        },
        "attribute": "description-field",
        "reflect": false
      },
      "entityName": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Nome da entidade representada na grade"
        },
        "attribute": "entity-name",
        "reflect": false
      },
      "shouldLoadConfig": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Define se deve carregar configura\u00E7\u00F5es da grade"
        },
        "attribute": "should-load-config",
        "reflect": false,
        "defaultValue": "false"
      }
    };
  }
  static get states() {
    return {
      "_gridConfig": {},
      "_inMemoryLoader": {},
      "_dataunit": {}
    };
  }
  static get events() {
    return [{
        "method": "pesquisaGridSelectItem",
        "name": "pesquisaGridSelectItem",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "IOption",
          "resolved": "IOption",
          "references": {
            "IOption": {
              "location": "import",
              "path": "@sankhyalabs/ezui/src/components/ez-search/ez-search"
            }
          }
        }
      }];
  }
  static get watchers() {
    return [{
        "propName": "dataSource",
        "methodName": "observeDataSource"
      }, {
        "propName": "metadata",
        "methodName": "observeMetadata"
      }, {
        "propName": "shouldLoadConfig",
        "methodName": "observeShouldLoadConfig"
      }];
  }
}
