"use strict";

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

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

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

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

var _reactRouter = require("react-router");

var _immutable = _interopRequireDefault(require("immutable"));

var _AccountPageContainer = _interopRequireDefault(require("../../containers/pages/AccountPageContainer"));

var _AuthRolePageContainer = _interopRequireDefault(require("../../containers/pages/AuthRolePageContainer"));

var _AdminNavBar = _interopRequireDefault(require("../admin/AdminNavBar"));

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

var _permissionHelpers = require("../../helpers/permissionHelpers");

var _AdminPage = _interopRequireDefault(require("../../../styles/cspace-ui/AdminPage.css"));

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

const messages = (0, _reactIntl.defineMessages)({
  title: {
    "id": "adminPage.title",
    "defaultMessage": "Administration"
  }
});
const tabs = ['account', 'authrole'];
const propTypes = {
  match: _propTypes.default.shape({
    url: _propTypes.default.string
  }),
  perms: _propTypes.default.instanceOf(_immutable.default.Map),
  preferredTab: _propTypes.default.string
};

function AdminPage(props) {
  const {
    match,
    perms,
    preferredTab
  } = props;
  const basename = match.url;

  const title = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.title);

  const permittedTabs = tabs.filter(tab => (0, _permissionHelpers.canList)(tab, perms));

  if (permittedTabs.length === 0) {
    return null;
  }

  const redirectTabName = preferredTab || permittedTabs[0];
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _AdminPage.default.common
  }, /*#__PURE__*/_react.default.createElement(_TitleBar.default, {
    title: title,
    updateDocumentTitle: true
  }), /*#__PURE__*/_react.default.createElement(_AdminNavBar.default, {
    basename: basename,
    tabs: permittedTabs
  }), /*#__PURE__*/_react.default.createElement(_reactRouter.Switch, null, /*#__PURE__*/_react.default.createElement(_reactRouter.Redirect, {
    exact: true,
    path: basename,
    to: `${basename}/${redirectTabName}`
  }), /*#__PURE__*/_react.default.createElement(_reactRouter.Route, {
    path: `${basename}/account/:csid?`,
    component: _AccountPageContainer.default
  }), /*#__PURE__*/_react.default.createElement(_reactRouter.Route, {
    path: `${basename}/authrole/:csid?`,
    component: _AuthRolePageContainer.default
  })));
}

AdminPage.propTypes = propTypes;