var __rest = (this && this.__rest) || function (s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
import { ApplicationContext, ObjectUtils } from "@sankhyalabs/core";
import { DataFetcher } from '../../../lib/http/data-fetcher/DataFetcher';
import { DataExporterOption } from "../../../lib/@types";
import { getOptionKey, getVisibleColumns } from "../utils/ParserExport";
export default class ServerSideExporterStrategy {
  constructor(provider) {
    this._provider = provider;
  }
  async getParsedColumns() {
    const columns = await this._provider.getColumnsMetadata();
    const visibleColumns = getVisibleColumns(columns);
    return visibleColumns;
  }
  async getColumns(resolveProps) {
    var _a;
    if ((_a = getOptionKey(resolveProps === null || resolveProps === void 0 ? void 0 : resolveProps.exportOption)) === null || _a === void 0 ? void 0 : _a.includes("PDF")) {
      return await this.getParsedColumns();
    }
    return await this._provider.getColumnsMetadata();
  }
  async getParams(resolveProps) {
    var _a, _b, _c, _d, _e, _f;
    const filters = (_b = (_a = this._provider).getFilters) === null || _b === void 0 ? void 0 : _b.call(_a);
    const columns = await this.getColumns(resolveProps);
    const sort = (_d = (_c = this._provider).getOrders) === null || _d === void 0 ? void 0 : _d.call(_c);
    const resourceURI = (_f = (_e = this._provider).getResourceURI) === null || _f === void 0 ? void 0 : _f.call(_e);
    const selectedIDs = this._provider.getSelectedIDs();
    const option = resolveProps.exportOption;
    const currentPageOptions = [
      DataExporterOption.EXPORT_PAGE_TO_PDF,
      DataExporterOption.EXPORT_PAGE_TO_XLS
    ];
    delete resolveProps.exportOption;
    let params = Object.assign({ filters,
      columns,
      sort,
      resourceURI, selectedIDs: selectedIDs.slice(0, this._provider.getExportLimit()) }, resolveProps);
    if (currentPageOptions.includes(option) || resolveProps.type == "page") {
      const offset = this._provider.getOffset();
      const limit = this._provider.getPageSize();
      params = Object.assign(Object.assign({}, params), { offset,
        limit, selectedIDs: [] });
    }
    else if (resolveProps.type == "all") {
      params = Object.assign(Object.assign({}, params), { offset: 0, limit: this._provider.getExportLimit(), selectedIDs: [] });
    }
    return Promise.resolve(params);
  }
  async executeExport(resolveProps) {
    const _a = await this.getParams(resolveProps), { methodName } = _a, requestBody = __rest(_a, ["methodName"]);
    const application = ApplicationContext.getContextValue("__SNK__APPLICATION__");
    const serviceName = `${application.getModuleName()}@DataExporterSPBean.${methodName}`;
    const payload = { serviceName, requestBody };
    return new Promise((resolve, reject) => {
      DataFetcher.get()
        .callServiceBroker(serviceName, ObjectUtils.objectToString(payload))
        .then(result => resolve(this.getFormatResponse(result)))
        .catch(error => reject(error));
    });
  }
  getFormatResponse(result) {
    var _a;
    const response = (_a = result === null || result === void 0 ? void 0 : result.json) === null || _a === void 0 ? void 0 : _a.$;
    if (response == undefined) {
      return;
    }
    return ObjectUtils.stringToObject(response);
  }
}
