var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { LogUtility } from '../../../../utils/log';
/**
 * The dataSource controller for the vega-table
 */
export class VegaTableDataSourceController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Get the key of the record
     *
     * @param {VegaTableDataSourceItem} record - The record of the dataSource
     * @returns {string} The key of the record
     */
    this.getRecordKey = (record) => {
      if (record[this.rowKey] !== undefined && record[this.rowKey] !== null) {
        return String(record[this.rowKey]);
      }
      else {
        return '';
      }
    };
    /**
     * Get the record from [currentData]{@link currentData} by key
     *
     * @param {string} key - The key of the record
     * @returns {VegaTableDataSourceItem} The record of the [currentData]{@link currentData}
     */
    this.getRecordByKey = (key) => {
      return this.currentData.find((record) => this.getRecordKey(record) === key);
    };
    /**
     * Update the [currentData]{@link currentData} by new data source
     *
     * @param {VegaTableDataSourceItem[]} data - New data source
     */
    this.setCurrentData = (data) => {
      if (this.tableDataValidation(data)) {
        this.currentData = data;
      }
      else {
        this.currentData = [];
      }
    };
  }
  /**
   * Component lifecycle - componentWillLoad
   */
  componentWillLoad() {
    this.vegaTableSortController.syncSortOrder();
    this.syncCurrentData();
  }
  /**
   * The callback of [watchSortersMap]{@link VegaTable.watchSortersMap}
   */
  watchSortersMap() {
    this.syncCurrentData();
  }
  /**
   * The callback of watchDataSource
   */
  watchDataSource() {
    this.syncCurrentData();
  }
  /**
   * Get currentData item keys
   *
   * @returns {string[]} The key array of currentData
   */
  getAllRecordKeys() {
    return this.currentData.map(this.getRecordKey);
  }
  /**
   * The getter of dataSource
   *
   * @returns {VegaTableDataSourceItem[]} The value of dataSource
   */
  getDataSource() {
    return this.dataSource;
  }
  /**
   * The setter of [dataSource]{@link dataSource}
   *
   * @param {VegaTableDataSourceItem[]} data - The new data source
   */
  setDataSource(data) {
    this.dataSource = data;
  }
  /**
   * The getter of [currentData]{@link currentData}
   *
   * @returns {VegaTableDataSourceItem[]} the value of currentData
   */
  getCurrentData() {
    return this.currentData;
  }
  /**
   * Check whether the currentData is empty
   *
   * @returns {boolean} return true if the currentData is empty
   */
  isCurrentDataEmpty() {
    return this.currentData.length === 0;
  }
  /**
   * Verify whether the data is valid
   *
   * @param {VegaTableDataSourceItem[]} data - The data source
   * @returns {boolean} Return true if the data is valid, otherwise false
   */
  tableDataValidation(data) {
    if (data.some((item) => !this.getRecordKey(item))) {
      LogUtility.error('Each item in dataSource should have a unique "key" prop.', ' Otherwise, you should specify the primary key of dataSource item via rowKey.', ' For more details, please refer to: https://heartlandpayments.github.io/Vega/?path=/docs/components-table-v2--basic-table#rowkey');
      return false;
    }
    return true;
  }
  /**
   * Update the currentData with sorted data if there is sorted status.
   * Otherwise, update with the dataSource directly
   */
  syncCurrentData() {
    if (this.isTemplateRenderMode) {
      return;
    }
    const sortedColumns = this.sortersMap.size > 0
      ? this.columns.filter((column) => column.sorter && column.sorter !== 'event' && this.sortersMap.has(column.prop))
      : [];
    if (sortedColumns.length > 0) {
      this.setCurrentData(this.vegaTableSortController.sortData(this.dataSource, Array.from(this.sortersMap.values())));
    }
    else {
      this.setCurrentData(this.dataSource);
    }
  }
}
__decorate([
  MapToComponentField()
], VegaTableDataSourceController.prototype, "rowKey", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableDataSourceController.prototype, "currentData", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTableDataSourceController.prototype, "dataSource", void 0);
__decorate([
  MapToComponentField()
], VegaTableDataSourceController.prototype, "columns", void 0);
__decorate([
  MapToComponentField()
], VegaTableDataSourceController.prototype, "sortersMap", void 0);
__decorate([
  MapToComponentField()
], VegaTableDataSourceController.prototype, "vegaTableSortController", void 0);
__decorate([
  MapToComponentField()
], VegaTableDataSourceController.prototype, "vegaTableRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaTableDataSourceController.prototype, "isTemplateRenderMode", void 0);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaTableDataSourceController.prototype, "componentWillLoad", null);
__decorate([
  MapToComponentMethod('watchSortersMap')
], VegaTableDataSourceController.prototype, "watchSortersMap", null);
__decorate([
  MapToComponentMethod('watchDataSource')
], VegaTableDataSourceController.prototype, "watchDataSource", null);
