import { CommonsExporter } from "../interfaces/IExporterProvider";
import { UserInterface } from "@sankhyalabs/core";
import { getSelectedRecordsIDsInfo as getSelectedIDs } from '../../../lib/utils/GetSelectedRecordsIDsInfo';
import { DataExporterOption } from "../../../lib/@types";
import { IExportType } from "../../../lib/http/data-fetcher/fetchers/fetchDataExporter/interfaces/IFetchDataExporterParams";
export default class ClientSideExporterProvider extends CommonsExporter {
  getSelectedNumber() {
    return this.dataUnit.getSelectionInfo().length;
  }
  getTotalRecords() {
    var _a, _b, _c;
    const { total } = ((_a = this.dataUnit) === null || _a === void 0 ? void 0 : _a.getPaginationInfo()) || {};
    return total !== null && total !== void 0 ? total : (_c = (_b = this.dataUnit) === null || _b === void 0 ? void 0 : _b.records) === null || _c === void 0 ? void 0 : _c.length;
  }
  getSelectedIDs() {
    return getSelectedIDs(this.dataUnit);
  }
  getRecordID() {
    var _a, _b, _c;
    return (_c = (_b = (_a = this.dataUnit) === null || _a === void 0 ? void 0 : _a.records) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.__record__id__;
  }
  async getRecords(exportType) {
    if (this.dataUnit.records.length === 0)
      return [];
    switch (exportType) {
      case IExportType.ALL:
        return this.dataUnit.getSelectionInfo().getAllRecords();
      case IExportType.SELECTION:
        return this.resolveRecordsFromSelection();
      case IExportType.PAGE:
        return this.dataUnit.records;
      default:
        return this.resolveRecordsFromSelection();
    }
  }
  resolveRecordsFromSelection() {
    const selectionInfo = this.dataUnit.getSelectionInfo();
    if (selectionInfo.isEmpty()) {
      return this.dataUnit.records;
    }
    const selectionRecords = selectionInfo.isAllRecords() ? selectionInfo.getAllRecords() : selectionInfo.records;
    return selectionRecords.length ? selectionRecords : this.dataUnit.records;
  }
  getHiddenOptions() {
    return [
      DataExporterOption.EXPORT_BY_EMAIL,
      DataExporterOption.EXPORT_PDF_TO_EMAIL,
      DataExporterOption.EXPORT_XLS_TO_EMAIL,
      DataExporterOption.EXPORT_PAGE_TO_PDF,
      DataExporterOption.EXPORT_PAGE_TO_XLS
    ];
  }
  formatValue(record, column) {
    const { id, descriptionFrom } = column;
    const value = record[descriptionFrom || id];
    if (value == undefined) {
      return "";
    }
    if (descriptionFrom != undefined) {
      return value.label;
    }
    const fieldDescriptor = this.dataUnit.getField(id);
    if (fieldDescriptor.userInterface === UserInterface.SEARCH) {
      return value.value;
    }
    if (column.customFormatter) {
      const ezGridColumn = this.getColumnsState().find(columnState => column.id === columnState.name);
      const formattedValue = column.customFormatter.format(value, ezGridColumn, record.__record__id__);
      return formattedValue;
    }
    return this.dataUnit.getFormattedValue(id, value);
  }
}
