'use strict';

require('./_commonjsHelpers-06173234.js');
require('./_to-object-329a650b.js');
require('./es6.string.iterator-cc0bbaa4.js');
require('./core.get-iterator-method-f62321d4.js');
require('./web.dom.iterable-a0e279c1.js');
var React = require('react');
var slicedToArray = require('./slicedToArray-8f385299.js');
var reactBootstrap = require('react-bootstrap');
var styleInject_es = require('./style-inject.es-dcee06b6.js');
var nextAuth = require('next-auth/client');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var nextAuth__default = /*#__PURE__*/_interopDefaultLegacy(nextAuth);

var css = ".AuthWrapper-module_loginBox__3l-BE {\n  margin-bottom: 2rem;\n  width: 350px;\n  max-width: 90vw;\n  margin-left: auto;\n  margin-right: auto;\n  /* box-shadow: 2px 5px 12px rgba(0, 0, 0, 0.15); */\n  background: #fff;\n  z-index: 1;\n}\n.AuthWrapper-module_loginBox_title__12rGD {\n  margin-bottom: 2rem;\n  text-align: center;\n}\n.AuthWrapper-module_bar__1dafn {\n  background: #eee;\n  margin-left: unset;\n  margin-right: unset;\n}\n.AuthWrapper-module_bar__1dafn:nth-child(1) {\n  float: left;\n  border-right: 1rem solid #fff;\n  background: rgb(245, 245, 245);\n  height: 10rem;\n  width: 25%;\n  margin-left: auto;\n  margin-right: auto;\n  margin-bottom: 2rem;\n}\n.AuthWrapper-module_bar__1dafn:nth-child(2n) {\n  width: 97%;\n  height: 1.5rem;\n  margin-bottom: 0.25rem;\n}\n.AuthWrapper-module_bar__1dafn.AuthWrapper-module_bar__1dafn:nth-child(3n) {\n  width: 96%;\n  height: 1.5rem;\n  margin-bottom: 1.5rem;\n}\n.AuthWrapper-module_bar__1dafn.AuthWrapper-module_bar__1dafn:nth-child(4n) {\n  width: 72%;\n  height: 1.5rem;\n  margin-bottom: 0.25rem;\n}\n.AuthWrapper-module_bar__1dafn.AuthWrapper-module_bar__1dafn:nth-child(5n) {\n  width: 88%;\n  height: 1.5rem;\n  margin-bottom: 0.25rem;\n}\n\n.AuthWrapper-module_iframe__2wDJW {\n  margin-top: -3rem;\n  border: none;\n  width: 100%;\n  height: 1900px;\n}";
var styles = { "loginBox": "AuthWrapper-module_loginBox__3l-BE", "loginBox_title": "AuthWrapper-module_loginBox_title__12rGD", "bar": "AuthWrapper-module_bar__1dafn", "iframe": "AuthWrapper-module_iframe__2wDJW" };
styleInject_es.styleInject(css);

var AuthWrapper = function AuthWrapper(_ref) {
  var children = _ref.children,
      needAuth = _ref.needAuth,
      _ref$formUrl = _ref.formUrl,
      formUrl = _ref$formUrl === undefined ? '' : _ref$formUrl;

  var _nextAuth$useSession = nextAuth__default['default'].useSession(),
      _nextAuth$useSession2 = slicedToArray._slicedToArray(_nextAuth$useSession, 2),
      session = _nextAuth$useSession2[0],
      loading = _nextAuth$useSession2[1];

  var _useState = React.useState(null),
      _useState2 = slicedToArray._slicedToArray(_useState, 2),
      callbackUrl = _useState2[0],
      setCallbackUrl = _useState2[1];

  var _useState3 = React.useState(),
      _useState4 = slicedToArray._slicedToArray(_useState3, 2),
      email = _useState4[0],
      setEmail = _useState4[1];

  var _useState5 = React.useState(false),
      _useState6 = slicedToArray._slicedToArray(_useState5, 2),
      showForm = _useState6[0],
      setShowForm = _useState6[1];

  var _useState7 = React.useState(),
      _useState8 = slicedToArray._slicedToArray(_useState7, 2),
      password = _useState8[0],
      setPassword = _useState8[1];

  React.useEffect(function () {
    setCallbackUrl(window.location.href);
  }, []);

  if (session || !needAuth) {
    return React__default['default'].createElement(
      'div',
      null,
      children
    );
  }

  if (!loading) {
    return React__default['default'].createElement(
      'div',
      { style: { position: 'relative', minHeight: '300px', paddingTop: '2rem' } },
      callbackUrl && React__default['default'].createElement(
        'div',
        null,
        !showForm && React__default['default'].createElement(
          'div',
          { className: styles.loginBox },
          React__default['default'].createElement(
            'div',
            { className: styles.loginBox_title },
            React__default['default'].createElement(
              'b',
              null,
              'Please log in to view this content.'
            )
          ),
          React__default['default'].createElement(
            reactBootstrap.Form,
            { onSubmit: function onSubmit(e) {
                return e.preventDefault();
              } },
            React__default['default'].createElement(
              reactBootstrap.Form.Group,
              { controlId: 'email' },
              React__default['default'].createElement(reactBootstrap.Form.Control, { type: 'email', onChange: function onChange(e) {
                  return setEmail(e.target.value);
                }, placeholder: 'Email' })
            ),
            React__default['default'].createElement(
              reactBootstrap.Form.Group,
              { controlId: 'password' },
              React__default['default'].createElement(reactBootstrap.Form.Control, { type: 'password', onChange: function onChange(e) {
                  return setPassword(e.target.value);
                }, placeholder: 'Password' })
            ),
            React__default['default'].createElement(
              'button',
              { className: 'btn btn-primary btn-block', onClick: function onClick() {
                  return nextAuth__default['default'].signIn('credentials', { email: email, password: password, callbackUrl: callbackUrl });
                } },
              'Sign in'
            )
          ),
          React__default['default'].createElement(
            'p',
            { style: { textAlign: 'center' }, className: 'mt-2 mb-0' },
            'Don\'t have an account?',
            ' ',
            React__default['default'].createElement(
              'a',
              { style: { color: 'blue', textDecoration: 'underline' }, onClick: function onClick() {
                  return setShowForm(!showForm);
                } },
              'Click Here'
            ),
            ' to register.'
          ),
          React__default['default'].createElement(
            'p',
            { className: 'mt-1', style: { fontSize: '.8em', textAlign: 'center' } },
            React__default['default'].createElement(
              'em',
              null,
              React__default['default'].createElement(
                'a',
                { href: '/forgotpassword', style: { color: 'blue', textDecoration: 'underline' } },
                'Forgot your password? '
              )
            )
          )
        ),
        showForm && React__default['default'].createElement('iframe', { className: styles.iframe, src: formUrl })
      )
    );
  } else {
    return null;
  }
};

module.exports = AuthWrapper;
