"use strict";

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

var _reactIntl = require("react-intl");

var _get = _interopRequireDefault(require("lodash/get"));

var _session = _interopRequireDefault(require("../helpers/session"));

var _getErrorDescription = _interopRequireDefault(require("../helpers/getErrorDescription"));

var _notificationHelpers = _interopRequireDefault(require("../helpers/notificationHelpers"));

var _recordDataHelpers = require("../helpers/recordDataHelpers");

var _validationHelpers = require("../helpers/validationHelpers");

var _invocationHelpers = require("../helpers/invocationHelpers");

var _record = require("./record");

var _notification = require("./notification");

var _notificationStatusCodes = require("../constants/notificationStatusCodes");

var _reducers = require("../reducers");

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

/* global window */
const messages = (0, _reactIntl.defineMessages)({
  error: {
    "id": "report.error",
    "defaultMessage": "Error running report: {error}"
  }
});

const openReport = (config, reportMetadata, invocationDescriptor) => (dispatch, getState) => {
  const reportCsid = (0, _recordDataHelpers.getCsid)(reportMetadata);
  const reportNameGetter = (0, _get.default)(config, ['recordTypes', 'report', 'invocableName']);
  const reportName = reportNameGetter && reportNameGetter(reportMetadata);
  const paramRecordTypeConfig = (0, _get.default)(config, ['invocables', 'report', reportName]);
  const paramRecordCsid = '';
  let params;
  let validateParams;

  if (paramRecordTypeConfig) {
    validateParams = dispatch((0, _record.validateRecordData)(paramRecordTypeConfig, paramRecordCsid)).then(() => {
      if ((0, _validationHelpers.hasBlockingError)((0, _reducers.getRecordValidationErrors)(getState(), paramRecordCsid))) {
        return Promise.reject();
      }

      const data = (0, _reducers.getNewRecordData)(getState());
      params = data && data.first().toJS();
      return Promise.resolve();
    });
  } else {
    validateParams = Promise.resolve();
  }

  return validateParams.then(() => {
    const viewerPath = (0, _invocationHelpers.getReportViewerPath)(config, reportCsid, invocationDescriptor, params);
    window.open(viewerPath, _invocationHelpers.VIEWER_WINDOW_NAME);
  });
};

exports.openReport = openReport;

const invoke = (config, csid, invocationDescriptor, params) => dispatch => {
  const requestConfig = {
    data: (0, _invocationHelpers.createInvocationData)(config, invocationDescriptor, params),
    responseType: 'blob'
  };
  return (0, _session.default)().create(`reports/${csid}/invoke`, requestConfig).catch(error => {
    const notificationID = (0, _notificationHelpers.default)();
    dispatch((0, _notification.showNotification)({
      items: [{
        message: messages.error,
        values: {
          error: (0, _getErrorDescription.default)(error)
        }
      }],
      date: new Date(),
      status: _notificationStatusCodes.STATUS_ERROR
    }, notificationID));
    throw error;
  });
};

exports.invoke = invoke;