"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnauthenticatedUserModal = exports.EViewingScreen = void 0;
const contexts_1 = require("../../../../contexts/index.js");
const react_1 = __importStar(require("react"));
const AutoAutoHeight_1 = __importDefault(require("../../../../animations/AutoAutoHeight"));
const Fade_1 = __importDefault(require("../../../../animations/Fade"));
const Overlay_1 = require("../../Overlay/Overlay");
const ScreenSize_1 = __importDefault(require("../../../../util/ScreenSize"));
const SimpleModal_1 = __importStar(require("../SimpleModal"));
const ForgotPasswordFormContent_1 = require("./FormContent/ForgotPasswordFormContent");
const SignInFormContent_1 = require("./FormContent/SignInFormContent");
const SignUpFormContent_1 = require("./FormContent/SignUpFormContent");
var EViewingScreen;
(function (EViewingScreen) {
    EViewingScreen["Default"] = "Default";
    EViewingScreen["SignIn"] = "SignIn";
    EViewingScreen["SignUp"] = "SignUp";
    EViewingScreen["ForgotPassword"] = "ForgotPassword";
})(EViewingScreen || (exports.EViewingScreen = EViewingScreen = {}));
const UnauthenticatedUserModal = (props) => {
    const { open, anchorEl, placement, fallbackPlacements, containerEl, onClose, onOpenOnMobile, onConfirm } = props;
    const { cognitoLoginState, reset: cognitoReset } = (0, react_1.useContext)(contexts_1.AuthenticationContext);
    const [viewingScreen, setViewingScreen] = (0, react_1.useState)(EViewingScreen.SignIn);
    const nextViewingScreen = (0, react_1.useRef)(null);
    const { screenSize } = (0, react_1.useContext)(contexts_1.BookingEngineContext);
    const shouldBreakpoint = screenSize <= ScreenSize_1.default.Large || !anchorEl;
    const _onOpen = () => {
        if (shouldBreakpoint)
            onOpenOnMobile === null || onOpenOnMobile === void 0 ? void 0 : onOpenOnMobile();
    };
    const onDone = () => {
        _onOpen(); // It is unclear why onOpen is being called, but it is kept to avoid introducing regressions.
        onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm();
    };
    (0, react_1.useEffect)(() => {
        if (!open)
            return;
        if (cognitoLoginState === contexts_1.CognitoLoginState.RequiresConfirmation) {
            setViewingScreen(EViewingScreen.SignUp);
        }
        else {
            setViewingScreen(EViewingScreen.SignIn);
        }
    }, [open]);
    const reset = () => {
        cognitoReset();
        setViewingScreen(null);
    };
    const showForgotPassword = () => {
        reset();
        nextViewingScreen.current = EViewingScreen.ForgotPassword;
    };
    const showSignInFormContent = () => {
        reset();
        nextViewingScreen.current = EViewingScreen.SignIn;
    };
    const showSignUpFormContent = () => {
        reset();
        nextViewingScreen.current = EViewingScreen.SignUp;
    };
    const onAnimationExit = () => {
        setViewingScreen(nextViewingScreen.current);
    };
    const innerContent = (react_1.default.createElement("div", { className: "u-w-100 u-pad--heavy@m u-pad@m-" },
        react_1.default.createElement(AutoAutoHeight_1.default, { open: true, startOpen: true }, [
            [
                EViewingScreen.SignIn,
                react_1.default.createElement(SignInFormContent_1.SignInFormContent, { key: EViewingScreen.SignIn, onForgotPasswordClick: showForgotPassword, onSignUpClick: showSignUpFormContent, onDone: onDone, onNotVerified: showSignUpFormContent }),
            ],
            [EViewingScreen.SignUp, react_1.default.createElement(SignUpFormContent_1.SignUpFormContent, { key: EViewingScreen.SignUp, onSignInClick: showSignInFormContent, onDone: onDone })],
            [
                EViewingScreen.ForgotPassword,
                react_1.default.createElement(ForgotPasswordFormContent_1.ForgotPasswordFormContent, { key: EViewingScreen.ForgotPassword, onCancelled: showSignInFormContent, onDone: onDone, onNotVerified: showSignUpFormContent }),
            ],
        ].map(([forScreen, content], index) => (react_1.default.createElement(Fade_1.default, { open: viewingScreen === forScreen, onExitCompletely: onAnimationExit, key: index }, content))))));
    if (shouldBreakpoint) {
        return (react_1.default.createElement(SimpleModal_1.default, { open: open, onClose: onClose, size: SimpleModal_1.SimpleModalSize.Small }, innerContent));
    }
    else {
        return (react_1.default.createElement(Overlay_1.Overlay, { usePortal: true, followElement: shouldBreakpoint ? null : anchorEl, placement: placement || 'auto-end', fallbackPlacements: fallbackPlacements, container: containerEl, open: open, onClose: onClose, strategy: "fixed", className: "c-login-overlay" }, innerContent));
    }
};
exports.UnauthenticatedUserModal = UnauthenticatedUserModal;
//# sourceMappingURL=UnauthenticatedUserModal.js.map