"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

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

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

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

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message"));

var 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)("Aroha mai, he hapa i " + error.response.status + "."));
    } else {
      dispatch((0, _alerts.addError)("Aroha mai, i reira he hapa."));
    }
  };
};

exports.handleError = handleError;