"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 _VocabularyPageContainer = _interopRequireDefault(require("../../containers/pages/VocabularyPageContainer"));

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 = ['vocabulary', 'account', 'authrole'];
const propTypes = {
  match: _propTypes.default.object,
  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 = _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 _react.default.createElement("div", {
    className: _AdminPage.default.common
  }, _react.default.createElement(_TitleBar.default, {
    title: title,
    updateDocumentTitle: true
  }), _react.default.createElement(_AdminNavBar.default, {
    basename: basename,
    tabs: permittedTabs
  }), _react.default.createElement(_reactRouter.Switch, null, _react.default.createElement(_reactRouter.Redirect, {
    exact: true,
    path: basename,
    to: "".concat(basename, "/").concat(redirectTabName)
  }), _react.default.createElement(_reactRouter.Route, {
    path: "".concat(basename, "/vocabulary/:csid?"),
    component: _VocabularyPageContainer.default
  }), _react.default.createElement(_reactRouter.Route, {
    path: "".concat(basename, "/account/:csid?"),
    component: _AccountPageContainer.default
  }), _react.default.createElement(_reactRouter.Route, {
    path: "".concat(basename, "/authrole/:csid?"),
    component: _AuthRolePageContainer.default
  })));
}

AdminPage.propTypes = propTypes;