"use strict";

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

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

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

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

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

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

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

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

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

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

const openExport = (config, invocationDescriptor) => () => {
  (0, _invocationHelpers.storeInvocationDescriptor)(invocationDescriptor);
  const viewerPath = (0, _invocationHelpers.getExportViewerPath)(config, invocationDescriptor);
  return new Promise(resolve => {
    window.open(viewerPath, _invocationHelpers.VIEWER_WINDOW_NAME);
    resolve();
  });
};

exports.openExport = openExport;

const invoke = (config, invocationDescriptor) => dispatch => {
  const requestConfig = {
    data: (0, _invocationHelpers.createInvocationData)(config, invocationDescriptor),
    responseType: 'blob'
  };
  return (0, _session.default)().create('exports', 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;