"use strict";

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

var _backendModel = require("../../../model/backendModel");

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

/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
const uiToBackendReportDefinition = reportDefinition => {
  const {
    report_params: {
      report_name: reportName,
      description,
      report_source: reportSource,
      core_params: {
        base_url: baseUrl,
        time_duration: timeDuration,
        report_format: reportFormat,
        header,
        footer,
        limit,
        origin
      }
    },
    trigger,
    delivery
  } = reportDefinition;
  let backendReportDefinition = {
    name: reportName,
    isEnabled: getBackendIsEnabled(trigger),
    source: {
      description: description,
      type: getBackendReportSource(reportSource),
      id: getBackendReportSourceId(baseUrl),
      origin: origin
    },
    format: {
      duration: timeDuration,
      fileFormat: getBackendReportFormat(reportFormat),
      ...(limit && {
        limit: limit
      }),
      ...(header && {
        header: header
      }),
      ...(footer && {
        footer: footer
      })
    },
    trigger: getBackendTrigger(trigger),
    ...(getBackendDelivery(delivery) && {
      delivery: getBackendDelivery(delivery)
    })
  };
  return backendReportDefinition;
};

exports.uiToBackendReportDefinition = uiToBackendReportDefinition;

const getBackendIsEnabled = trigger => {
  let enabled = true;

  if (trigger.trigger_params) {
    enabled = trigger.trigger_params.enabled;
  }

  return enabled;
};

const getBackendDelivery = delivery => {
  const {
    configIds: configIds,
    title: title,
    textDescription: textDescription,
    htmlDescription: htmlDescription
  } = delivery;
  let res = {
    configIds: configIds,
    title: title,
    textDescription: textDescription,
    htmlDescription: htmlDescription
  };
  return res;
};

const getBackendTrigger = trigger => {
  const {
    trigger_params: scheduleParams
  } = trigger;
  const {
    schedule
  } = { ...scheduleParams
  };
  let res = {
    triggerType: scheduleParams ? getBackendTriggerType(scheduleParams) : _backendModel.BACKEND_TRIGGER_TYPE.onDemand,
    schedule: schedule
  };
  return res;
};

const getBackendTriggerType = scheduleParams => {
  const {
    schedule_type: scheduleType
  } = scheduleParams;
  let res;

  switch (scheduleType) {
    case _constants.SCHEDULE_TYPE.cron:
      res = _backendModel.BACKEND_TRIGGER_TYPE.cronSchedule;
      break;

    case _constants.SCHEDULE_TYPE.recurring:
      res = _backendModel.BACKEND_TRIGGER_TYPE.intervalSchedule;
      break;
  }

  return res;
};

const getBackendReportFormat = reportFormat => {
  return _backendModel.REPORT_FORMAT_DICT[reportFormat];
};

const getBackendReportState = reportState => {
  return _backendModel.REPORT_STATE_DICT[reportState];
};

exports.getBackendReportState = getBackendReportState;

const getBackendReportSource = reportSource => {
  return _backendModel.REPORT_SOURCE_DICT[reportSource];
}; //TODO: tmp solution, we are extracting the id from the baseUrl, e.g. /app/dashboards#/view/<id>
// since currently dashboard/visualization id are not required in the UI model, will add in the future


exports.getBackendReportSource = getBackendReportSource;

const getBackendReportSourceId = baseUrl => {
  const id = baseUrl.split('/').pop() || '';
  return id;
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 10368 characters]