'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _Header = require('../sections/Header');

var _Header2 = _interopRequireDefault(_Header);

var _Footer = require('../sections/Footer');

var _Footer2 = _interopRequireDefault(_Footer);

var _LoginModal = require('../login/LoginModal');

var _LoginModal2 = _interopRequireDefault(_LoginModal);

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

/* global window */

const propTypes = {
  decorated: _propTypes2.default.bool,
  history: _propTypes2.default.object,
  openModalName: _propTypes2.default.string,
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  screenName: _propTypes2.default.string,
  username: _propTypes2.default.string.isRequired,
  children: _propTypes2.default.node,
  closeModal: _propTypes2.default.func,
  resetLogin: _propTypes2.default.func
};

const defaultProps = {
  decorated: true
};

class ProtectedPage extends _react.Component {
  constructor() {
    super();

    this.handleLoginSuccess = this.handleLoginSuccess.bind(this);
  }

  handleLoginSuccess() {
    var _props = this.props;
    const closeModal = _props.closeModal,
          resetLogin = _props.resetLogin;


    window.setTimeout(() => {
      if (resetLogin) {
        resetLogin();
      }

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

  render() {
    var _props2 = this.props;
    const decorated = _props2.decorated,
          history = _props2.history,
          openModalName = _props2.openModalName,
          perms = _props2.perms,
          screenName = _props2.screenName,
          username = _props2.username,
          children = _props2.children,
          closeModal = _props2.closeModal;


    const header = decorated ? _react2.default.createElement(_Header2.default, { history: history, perms: perms, screenName: screenName || username }) : null;

    const footer = decorated ? _react2.default.createElement(_Footer2.default, null) : null;

    return _react2.default.createElement(
      'div',
      null,
      header,
      children,
      footer,
      _react2.default.createElement(_LoginModal2.default, {
        isOpen: openModalName === _LoginModal2.default.modalName,
        onCloseButtonClick: closeModal,
        onSuccess: this.handleLoginSuccess
      })
    );
  }
}

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