"use strict";

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

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

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

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

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

var _modalNames = require("../../constants/modalNames");

var _FooterContainer = _interopRequireDefault(require("../../containers/sections/FooterContainer"));

var _Header = _interopRequireDefault(require("../sections/Header"));

var _LoginModal = _interopRequireDefault(require("../login/LoginModal"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/* global window */
const propTypes = {
  decorated: _propTypes.default.bool,
  history: _propTypes.default.shape({
    push: _propTypes.default.func
  }),
  openModalName: _propTypes.default.string,
  perms: _propTypes.default.instanceOf(_immutable.default.Map),
  screenName: _propTypes.default.string,
  username: _propTypes.default.string.isRequired,
  userPrefsLoaded: _propTypes.default.bool,
  children: _propTypes.default.node,
  closeModal: _propTypes.default.func,
  resetLogin: _propTypes.default.func
};
const defaultProps = {
  decorated: true
};
const contextTypes = {
  config: _propTypes.default.shape({
    pluginInfo: _propTypes.default.object,
    serverUrl: _propTypes.default.string
  }),
  intl: _reactIntl.intlShape
};

class ProtectedPage extends _react.Component {
  constructor() {
    super();
    this.handleLoginSuccess = this.handleLoginSuccess.bind(this);
  }

  handleLoginSuccess() {
    const {
      closeModal,
      resetLogin
    } = this.props;
    window.setTimeout(() => {
      if (resetLogin) {
        resetLogin();
      }

      if (closeModal) {
        closeModal();
      }
    }, 0);
  }

  render() {
    const {
      decorated,
      history,
      openModalName,
      perms,
      screenName,
      username,
      userPrefsLoaded,
      children,
      closeModal
    } = this.props;
    const {
      config,
      intl
    } = this.context;
    let header;

    if (decorated) {
      header = /*#__PURE__*/_react.default.createElement(_Header.default, {
        history: history,
        perms: perms,
        screenName: screenName || username,
        userPrefsLoaded: userPrefsLoaded
      });
    }

    const footer = decorated ? /*#__PURE__*/_react.default.createElement(_FooterContainer.default, {
      config: config,
      intl: intl
    }) : null;
    return /*#__PURE__*/_react.default.createElement("div", null, header, userPrefsLoaded ? children : null, footer, /*#__PURE__*/_react.default.createElement(_LoginModal.default, {
      isOpen: openModalName === _modalNames.MODAL_LOGIN,
      onCloseButtonClick: closeModal,
      onSuccess: this.handleLoginSuccess
    }));
  }

}

exports.default = ProtectedPage;
ProtectedPage.propTypes = propTypes;
ProtectedPage.defaultProps = defaultProps;
ProtectedPage.contextTypes = contextTypes;