import { UserInterface } from "@sankhyalabs/core";
import { DataFetcher } from "../../../lib";
import { getOptionKey, getVisibleColumns } from "../utils/ParserExport";
export default class ClientSideExporterStrategy {
  constructor(provider) {
    this._provider = provider;
  }
  async getParsedColumns() {
    const columns = await this._provider.getColumnsMetadata();
    const visibleColumns = getVisibleColumns(columns);
    return visibleColumns;
  }
  async executeExport(resolveProps) {
    var _a;
    const { methodName, gridTitle } = resolveProps;
    const records = await this._provider.getRecords(resolveProps.type);
    const columns = ((_a = getOptionKey(resolveProps === null || resolveProps === void 0 ? void 0 : resolveProps.exportOption)) === null || _a === void 0 ? void 0 : _a.includes("PDF")) ? await this.getParsedColumns() : await this._provider.getColumnsMetadata();
    const [serviceName, processor] = this.getExecutor(methodName);
    const payload = {
      serviceName,
      requestBody: {
        grid: {
          gridTitle,
          columns: this.buildColumnsObject(columns),
          rows: this.buildRowsObject(columns, records),
          customOptions: { xlsxExtension: false }
        }
      }
    };
    return new Promise((resolve, reject) => {
      DataFetcher.get()
        .callServiceBroker(serviceName, payload)
        .then(result => resolve(processor(result)))
        .catch(error => reject(error));
    });
  }
  getExecutor(methodName) {
    if (methodName === "exportToPDF") {
      return [
        "GridPDFBuilderSP.buildPDFFromJson",
        responseBody => ({
          fileSessionKey: responseBody.chavePDF.valor,
          canPrint: responseBody.permiteImprimir.valor === "S",
          canExport: responseBody.permiteExportar.valor === "S",
          canSendEmail: responseBody.canSendEmail.valor === "S",
          useAppPrint: true
        })
      ];
    }
    return [
      "GridXLSBuilderSP.buildXLSFromJson",
      responseBody => ({
        fileSessionKey: responseBody.chaveXLS.valor,
        canPrint: true,
        canExport: true,
        canSendEmail: true,
        useAppPrint: false,
      })
    ];
  }
  buildColumnsObject(columns) {
    return {
      column: columns.map((column, index) => {
        const { label, id, width, userInterface } = column;
        return Object.assign({ index: index + 1, id,
          label,
          width, presentationType: "P" }, (TYPE_MAP[userInterface] || { type: "S", align: "left" }));
      })
    };
  }
  buildRowsObject(columns, records) {
    return {
      row: records.map(rawRecord => {
        const record = {};
        columns.forEach((column, index) => record["c" + (index + 1)] = { $: this.formatValue(rawRecord, column) });
        return record;
      })
    };
  }
  formatValue(record, column) {
    if (this._provider.formatValue != undefined) {
      return this._provider.formatValue(record, column);
    }
    return record[column.id] || "";
  }
}
class FormatedRecord {
}
const TYPE_MAP = {
  [UserInterface.DATE]: { type: "D", align: "center" },
  [UserInterface.DATETIME]: { type: "H", align: "center" },
  [UserInterface.TIME]: { type: "I", align: "right" },
  [UserInterface.ELAPSEDTIME]: { type: "I", align: "right" },
  [UserInterface.DECIMALNUMBER]: { type: "F", align: "right" },
  [UserInterface.INTEGERNUMBER]: { type: "I", align: "right" },
  [UserInterface.SEARCH]: { type: "S", align: "right" }
};
