'use strict';

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

var _ui = require('../actions/ui');

var _alerts = require('../actions/alerts');

var _constants = require('@instructure/quiz-common/lib/constants');

var _formatMessage = require('@instructure/quiz-common/lib/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var handleError = exports.handleError = function handleError(dispatch, errorMessage) {
  return function (error) {
    if (error.response && [404, 401].includes(error.response.status)) {
      // need a timeout so focus is not stolen by
      // other callbacks after the alert opens
      setTimeout(function () {
        dispatch((0, _ui.set)(_constants.TRAY_ALERT_OPEN, true));
      }, 500);
    } else if (errorMessage) {
      dispatch((0, _alerts.addError)(errorMessage));
    } else if (error.response && error.response.status) {
      dispatch((0, _alerts.addError)('Aoha mai, he hapa i ' + error.response.status + '.'));
    } else {
      dispatch((0, _alerts.addError)('Sorry, there was an error.'));
    }
  };
};