"use strict";

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

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _get = _interopRequireDefault(require("lodash/get"));

var _TitleBar = _interopRequireDefault(require("../sections/TitleBar"));

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

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

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

function _extends() { _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; }; return _extends.apply(this, arguments); }

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

const propTypes = {
  error: _propTypes.default.object
};

function ErrorPage(props) {
  const {
    error
  } = props;
  let {
    code
  } = error;

  if (code === _errorCodes.ERR_API) {
    const status = (0, _get.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 || status === 403) {
      // Convert 401 and 403 to ERR_NOT_ALLOWED.
      code = _errorCodes.ERR_NOT_ALLOWED;
    }
  }

  const message = messages[code] || messages.error;
  return _react.default.createElement("div", {
    className: _ErrorPage.default.common
  }, _react.default.createElement(_TitleBar.default, {
    title: _react.default.createElement(_reactIntl.FormattedMessage, messages.title)
  }), _react.default.createElement("p", null, _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, message, {
    values: error
  }))));
}

ErrorPage.propTypes = propTypes;