'use strict';

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

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

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

var _constants = require('../../constants');

var _formatMessage = require('../../common/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

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)('Yn anffodus, roedd ' + error.response.status + ' Gwall.'));
    } else {
      dispatch((0, _alerts.addError)('Sorry, there was an error.'));
    }
  };
};

var handleBankError = exports.handleBankError = handleError;

var handleTagAssociationError = exports.handleTagAssociationError = handleError;