'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.invoke = exports.BATCH_INVOKE_REJECTED = exports.BATCH_INVOKE_FULFILLED = exports.BATCH_INVOKE_STARTED = 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 }; }

const BATCH_INVOKE_STARTED = exports.BATCH_INVOKE_STARTED = 'BATCH_INVOKE_STARTED';
const BATCH_INVOKE_FULFILLED = exports.BATCH_INVOKE_FULFILLED = 'BATCH_INVOKE_FULFILLED';
const BATCH_INVOKE_REJECTED = exports.BATCH_INVOKE_REJECTED = 'BATCH_INVOKE_REJECTED';

const 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}'
  }
});

const invoke = exports.invoke = (config, batchItem, invocationDescriptor) => dispatch => {
  const csid = batchItem.get('csid');
  const name = batchItem.get('name');

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

  dispatch({
    type: BATCH_INVOKE_STARTED,
    meta: {
      csid
    }
  });

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

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

  return (0, _cspace2.default)().create(`batch/${csid}`, requestConfig).then(response => {
    const data = response.data;

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

    let numAffectedInt;

    numAffectedInt = parseInt(numAffected, 10);

    if (isNaN(numAffectedInt)) {
      numAffectedInt = undefined;
    }

    dispatch({
      type: BATCH_INVOKE_FULFILLED,
      meta: {
        csid,
        numAffected: numAffectedInt
      }
    });

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

    return response;
  }).catch(error => {
    dispatch({
      type: BATCH_INVOKE_REJECTED,
      meta: {
        csid
      }
    });

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

exports.default = {};