'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _defineMessages;

exports.default = ErrorPage;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

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

var _get2 = _interopRequireDefault(_get);

var _TitleBar = require('../sections/TitleBar');

var _TitleBar2 = _interopRequireDefault(_TitleBar);

var _errorCodes = require('../../constants/errorCodes');

var _ErrorPage = require('../../../styles/cspace-ui/ErrorPage.css');

var _ErrorPage2 = _interopRequireDefault(_ErrorPage);

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var messages = (0, _reactIntl.defineMessages)((_defineMessages = {
  title: {
    'id': 'errorPage.title',
    'defaultMessage': 'Page Not Found'
  },
  error: {
    'id': 'errorPage.error',
    'defaultMessage': 'Error code: {code}'
  }
}, _defineProperty(_defineMessages, _errorCodes.ERR_NOT_ALLOWED, {
  'id': 'errorPage.ERR_NOT_ALLOWED',
  'defaultMessage': 'You\'re not allowed to view this type of record.'
}), _defineProperty(_defineMessages, _errorCodes.ERR_NOT_FOUND, {
  'id': 'errorPage.ERR_NOT_FOUND',
  'defaultMessage': 'The record you\'re looking for doesn\'t seem to exist.'
}), _defineProperty(_defineMessages, _errorCodes.ERR_MISSING_VOCABULARY, {
  'id': 'errorPage.ERR_MISSING_VOCABULARY',
  'defaultMessage': 'A vocabulary must be specified for the authority record type "{recordType}".'
}), _defineProperty(_defineMessages, _errorCodes.ERR_UNKNOWN_RECORD_TYPE, {
  'id': 'errorPage.ERR_UNKNOWN_RECORD_TYPE',
  'defaultMessage': 'There is no record type named "{recordType}".'
}), _defineProperty(_defineMessages, _errorCodes.ERR_UNKNOWN_VOCABULARY, {
  'id': 'errorPage.ERR_UNKNOWN_VOCABULARY',
  'defaultMessage': 'There is no vocabulary named "{vocabulary}" for the record type "{recordType}".'
}), _defineProperty(_defineMessages, _errorCodes.ERR_INVALID_CSID, {
  'id': 'errorPage.ERR_INVALID_CSID',
  'defaultMessage': '"{csid}" is not a valid CSID.'
}), _defineProperty(_defineMessages, _errorCodes.ERR_INVALID_RELATED_TYPE, {
  'id': 'errorPage.ERR_INVALID_RELATED_TYPE',
  'defaultMessage': 'The record type "{recordType}" does not have a related type "{relatedRecordType}".'
}), _defineProperty(_defineMessages, _errorCodes.ERR_UNKNOWN_SUBRESOURCE, {
  'id': 'errorPage.ERR_UNKNOWN_SUBRESOURCE',
  'defaultMessage': 'There is no subresource named "{subresource}".'
}), _defineMessages));

// TODO: The error prop should be an Immutable.Map. Most errors come from the store, so they are
// already immutable maps, which then have to be converted by the caller to an object before
// passing in to the error page. Errors coming from location validation are created as objects,
// but they can be changed to be created as immutable maps.

var propTypes = {
  error: _propTypes2.default.object
};

function ErrorPage(props) {
  var error = props.error;
  var code = error.code;


  if (code === _errorCodes.ERR_API) {
    var status = (0, _get2.default)(error, ['error', 'response', 'status']);

    if (status === 404) {
      // Convert 404 from the REST API into ERR_NOT_FOUND.
      code = _errorCodes.ERR_NOT_FOUND;
    } else if (status === 401) {
      // Convert 401 to ERR_NOT_ALLOWED.
      code = _errorCodes.ERR_NOT_ALLOWED;
    }
  }

  var message = messages[code] || messages.error;

  return _react2.default.createElement(
    'div',
    { className: _ErrorPage2.default.common },
    _react2.default.createElement(_TitleBar2.default, { title: _react2.default.createElement(_reactIntl.FormattedMessage, messages.title) }),
    _react2.default.createElement(
      'p',
      null,
      _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, message, { values: error }))
    )
  );
}

ErrorPage.propTypes = propTypes;