'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

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

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _VocabularyPageContainer = require('../../containers/pages/VocabularyPageContainer');

var _VocabularyPageContainer2 = _interopRequireDefault(_VocabularyPageContainer);

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

var _AccountPageContainer2 = _interopRequireDefault(_AccountPageContainer);

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

var _AuthRolePageContainer2 = _interopRequireDefault(_AuthRolePageContainer);

var _AdminNavBar = require('../admin/AdminNavBar');

var _AdminNavBar2 = _interopRequireDefault(_AdminNavBar);

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

var _TitleBar2 = _interopRequireDefault(_TitleBar);

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

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

var _AdminPage2 = _interopRequireDefault(_AdminPage);

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

var messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'adminPage.title',
    'defaultMessage': 'Administration'
  }
});

var tabs = ['vocabulary', 'account', 'authrole'];

var propTypes = {
  match: _propTypes2.default.object,
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  preferredTab: _propTypes2.default.string
};

function AdminPage(props) {
  var match = props.match,
      perms = props.perms,
      preferredTab = props.preferredTab;


  var basename = match.url;
  var title = _react2.default.createElement(_reactIntl.FormattedMessage, messages.title);
  var permittedTabs = tabs.filter(function (tab) {
    return (0, _permissionHelpers.canList)(tab, perms);
  });

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

  var redirectTabName = preferredTab || permittedTabs[0];

  return _react2.default.createElement(
    'div',
    { className: _AdminPage2.default.common },
    _react2.default.createElement(_TitleBar2.default, { title: title, updateDocumentTitle: true }),
    _react2.default.createElement(_AdminNavBar2.default, { basename: basename, tabs: permittedTabs }),
    _react2.default.createElement(
      _reactRouter.Switch,
      null,
      _react2.default.createElement(_reactRouter.Redirect, { exact: true, path: basename, to: basename + '/' + redirectTabName }),
      _react2.default.createElement(_reactRouter.Route, { path: basename + '/vocabulary/:csid?', component: _VocabularyPageContainer2.default }),
      _react2.default.createElement(_reactRouter.Route, { path: basename + '/account/:csid?', component: _AccountPageContainer2.default }),
      _react2.default.createElement(_reactRouter.Route, { path: basename + '/authrole/:csid?', component: _AuthRolePageContainer2.default })
    )
  );
}

AdminPage.propTypes = propTypes;