'use strict';

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

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

var _get2 = _interopRequireDefault(_get);

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

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

var _cspace2 = _interopRequireDefault(_cspace);

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

var _getErrorDescription2 = _interopRequireDefault(_getErrorDescription);

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

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

var _notificationHelpers2 = _interopRequireDefault(_notificationHelpers);

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

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

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

var messages = (0, _reactIntl.defineMessages)({
  running: {
    'id': 'batch.running',
    'defaultMessage': 'Running {name}\u2026'
  },
  complete: {
    'id': 'batch.complete',
    'defaultMessage': 'Completed {name}: {numAffected, plural, =0 {No records} one {# record} other {# records}} affected. {userNote}'
  },
  error: {
    'id': 'batch.error',
    'defaultMessage': 'Error running {name}: {error}'
  }
});

var invoke = exports.invoke = function invoke(config, batchItem, invocationDescriptor) {
  return function (dispatch) {
    var csid = batchItem.get('csid');
    var name = batchItem.get('name');

    var notificationID = (0, _notificationHelpers2.default)();

    dispatch((0, _notification.showNotification)({
      items: [{
        message: messages.running,
        values: {
          name: name
        }
      }],
      date: new Date(),
      status: _notificationStatusCodes.STATUS_PENDING
    }, notificationID));

    var requestConfig = {
      data: (0, _invocationHelpers.createInvocationData)(config, invocationDescriptor)
    };

    return (0, _cspace2.default)().create('batch/' + csid, requestConfig).then(function (response) {
      var data = response.data;

      var numAffected = (0, _get2.default)(data, ['ns2:invocationResults', 'numAffected']);
      var userNote = (0, _get2.default)(data, ['ns2:invocationResults', 'userNote']);

      dispatch((0, _notification.showNotification)({
        items: [{
          message: messages.complete,
          values: {
            name: name,
            numAffected: numAffected,
            userNote: userNote
          }
        }],
        date: new Date(),
        status: _notificationStatusCodes.STATUS_SUCCESS,
        autoClose: true
      }, notificationID));

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

exports.default = {};