import './_commonjsHelpers-0c4b6f40.js';
import './core.get-iterator-method-86d4db69.js';
import './web.dom.iterable-f8f967cd.js';
import React, { useState, useEffect } from 'react';
import { _ as _slicedToArray } from './slicedToArray-67a08c9c.js';
import { Form } from 'react-bootstrap';
import { s as styleInject } from './style-inject.es-1f59c1d0.js';
import nextAuth from 'next-auth/client';

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(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.useSession(),
      _nextAuth$useSession2 = _slicedToArray(_nextAuth$useSession, 2),
      session = _nextAuth$useSession2[0],
      loading = _nextAuth$useSession2[1];

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

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

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

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

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

  if (session || !needAuth) {
    return React.createElement(
      'div',
      null,
      children
    );
  }

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

export default AuthWrapper;
