"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createReport = void 0;

var _constants = require("../utils/constants");

var _savedSearchReportHelper = require("../utils/savedSearchReportHelper");

var _visualReportHelper = require("../utils/visual_report/visualReportHelper");

var _saveReport = require("./saveReport");

var _lodash = _interopRequireDefault(require("lodash"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
const createReport = async (request, context, report, config, savedReportId) => {
  const isScheduledTask = false; //@ts-ignore

  const logger = context.reporting_plugin.logger; //@ts-ignore

  const semaphore = context.reporting_plugin.semaphore; // @ts-ignore

  const opensearchReportsClient = context.reporting_plugin.opensearchReportsClient.asScoped(request);
  const opensearchClient = context.core.opensearch.legacy.client; // @ts-ignore

  const timezone = request.query.timezone; // @ts-ignore

  const dateFormat = request.query.dateFormat || _constants.DATA_REPORT_CONFIG.excelDateFormat; // @ts-ignore

  const csvSeparator = request.query.csvSeparator || ',';
  const protocol = config.get('osd_server', 'protocol');
  const hostname = config.get('osd_server', 'hostname');
  const port = config.get('osd_server', 'port');
  const basePath = config.osdConfig.get('server', 'basePath');
  let createReportResult;
  let reportId;
  const {
    report_definition: {
      report_params: reportParams
    }
  } = report;
  const {
    report_source: reportSource
  } = reportParams;

  try {
    // create new report instance and set report state to "pending"
    if (savedReportId) {
      reportId = savedReportId;
    } else {
      const opensearchResp = await (0, _saveReport.saveReport)(report, opensearchReportsClient);
      reportId = opensearchResp.reportInstance.id;
    } // generate report


    if (reportSource === _constants.REPORT_TYPE.savedSearch) {
      createReportResult = await (0, _savedSearchReportHelper.createSavedSearchReport)(report, opensearchClient, dateFormat, csvSeparator, isScheduledTask, logger);
    } else {
      // report source can only be one of [saved search, visualization, dashboard, notebook]
      // compose url
      const relativeUrl = report.query_url.startsWith(basePath) ? report.query_url : `${basePath}${report.query_url}`;
      const completeQueryUrl = `${protocol}://${hostname}:${port}${relativeUrl}`;

      const extraHeaders = _lodash.default.pick(request.headers, _constants.EXTRA_HEADERS);

      const [value, release] = await semaphore.acquire();

      try {
        createReportResult = await (0, _visualReportHelper.createVisualReport)(reportParams, completeQueryUrl, logger, extraHeaders, timezone);
      } finally {
        release();
      }
    } // update report state to "created"
    // TODO: temporarily remove the following
    // if (!savedReportId) {
    //   await updateReportState(reportId, opensearchReportsClient, REPORT_STATE.created);
    // }

  } catch (error) {
    // update report instance with "error" state
    // TODO: save error detail and display on UI
    // TODO: temporarily disable the following, will add back
    // if (!savedReportId) {
    //   await updateReportState(reportId, opensearchReportsClient, REPORT_STATE.error);
    // }
    throw error;
  }

  return createReportResult;
};

exports.createReport = createReport;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 9384 characters]