'use strict';

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

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

var _cspace = require('./cspace');

var _cspace2 = _interopRequireDefault(_cspace);

var _getErrorDescription = require('../helpers/getErrorDescription');

var _getErrorDescription2 = _interopRequireDefault(_getErrorDescription);

var _notificationHelpers = require('../helpers/notificationHelpers');

var _notificationHelpers2 = _interopRequireDefault(_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 }; }

const messages = (0, _reactIntl.defineMessages)({
  error: {
    'id': 'report.error',
    'defaultMessage': 'Error running report: {error}'
  }
});

const invoke = exports.invoke = (config, csid, invocationDescriptor) => dispatch => {
  const requestConfig = {
    data: (0, _invocationHelpers.createInvocationData)(config, invocationDescriptor),
    responseType: 'blob'
  };

  return (0, _cspace2.default)().create(`reports/${csid}`, requestConfig).catch(error => {
    const notificationID = (0, _notificationHelpers2.default)();

    dispatch((0, _notification.showNotification)({
      items: [{
        message: messages.error,
        values: {
          error: (0, _getErrorDescription2.default)(error)
        }
      }],
      date: new Date(),
      status: _notificationStatusCodes.STATUS_ERROR
    }, notificationID));

    throw error;
  });
};

exports.default = {};