'use strict';

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

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

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

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 _reportHelpers = require('../helpers/reportHelpers');

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 }; }

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

const openReport = exports.openReport = (reportItem, config, recordType, csid) => (dispatch, getState) => {
  const reportName = (0, _invocationHelpers.getReportName)(reportItem);
  const recordTypeConfig = (0, _get2.default)(config, ['invocables', 'report', reportName]);
  const paramsRecordCsid = '';

  let params;
  let validateParams;

  if (recordTypeConfig) {
    validateParams = dispatch((0, _record.validateRecordData)(recordTypeConfig, paramsRecordCsid)).then(() => {
      if ((0, _reducers.getRecordValidationErrors)(getState(), paramsRecordCsid)) {
        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, _reportHelpers.getReportViewerPath)(reportItem, config, recordType, csid, params);

    window.open(viewerPath, _reportHelpers.VIEWER_WINDOW_NAME);
  });
};

const invoke = exports.invoke = (config, csid, invocationDescriptor) => dispatch => (0, _cspace2.default)().read(`reports/${csid}`).then(response => {
  const filename = (0, _get2.default)(response, ['data', 'document', 'ns2:reports_common', 'filename']);
  const reportName = filename && filename.substring(0, filename.lastIndexOf('.'));

  const requestConfig = {
    data: (0, _invocationHelpers.createInvocationData)(config, invocationDescriptor, 'report', reportName),
    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 = {};