import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Button, useSafeLayoutEffect } from "@vertesia/ui/core";
import { Env } from "@vertesia/ui/env";
import { UserNotFoundError, useUserSession, useUXTracking } from "@vertesia/ui/session";
import clsx from "clsx";
import { useEffect, useState } from "react";
import EnterpriseSigninButton from "./EnterpriseSigninButton";
import GitHubSignInButton from "./GitHubSignInButton";
import GoogleSignInButton from "./GoogleSignInButton";
import MicrosoftSignInButton from "./MicrosoftSigninButton";
import SignupForm from "./SignupForm";
export function SigninScreen({ allowedPrefix, isNested = false, lightLogo, darkLogo }) {
    const [allow, setAllow] = useState(false);
    useSafeLayoutEffect(() => {
        allowedPrefix && setAllow(window.location.href.startsWith(allowedPrefix));
    }, []);
    return allow ? null : _jsx(SigninScreenImpl, { isNested: isNested, lightLogo: lightLogo, darkLogo: darkLogo });
}
function SigninScreenImpl({ isNested = false, lightLogo, darkLogo }) {
    const { isLoading, user, authError } = useUserSession();
    return !isLoading && !user ? (_jsx("div", { style: { zIndex: 999998 }, className: (isNested ? "absolute" : "fixed") + "overflow-y-auto ", children: _jsxs("div", { className: clsx("flex flex-col items-center justify-center py-14 px-4"), children: [_jsx(StandardSigninPanel, { authError: authError, lightLogo: lightLogo, darkLogo: darkLogo }), _jsxs("div", { className: "flex gap-x-6 mt-10 justify-center text-muted", children: [_jsx("a", { href: "https://vertesiahq.com/privacy", className: "text-sm", children: "Privacy Policy" }), _jsx("a", { href: "https://vertesiahq.com/terms", className: "text-sm", children: "Terms of Service" })] })] }) })) : null;
}
function StandardSigninPanel({ authError, darkLogo, lightLogo }) {
    const [signupData, setSignupData] = useState(undefined);
    const [collectSignupData, setCollectSignupData] = useState(false);
    const { signOut } = useUserSession();
    const { trackEvent } = useUXTracking();
    history.replaceState({}, '', '/');
    const goBack = () => {
        console.log("Going back, signing out");
        setSignupData(undefined);
        setCollectSignupData(false);
        signOut();
    };
    const goToSignup = () => {
        setSignupData(undefined);
        setCollectSignupData(true);
    };
    useEffect(() => {
        if (authError instanceof UserNotFoundError) {
            console.log("User not found, redirecting to signup");
            goToSignup();
        }
    }, [authError]);
    const onSignup = (data, fbToken) => {
        console.log("Got Signup data", data);
        setSignupData(data);
        const payload = {
            signupData: data,
            firebaseToken: fbToken,
        };
        fetch(Env.endpoints.studio + "/auth/signup", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(payload),
        }).then((res) => {
            console.log("Signup successful", payload, res);
            trackEvent("sign_up");
            window.location.href = "/";
        });
    };
    return (_jsxs(_Fragment, { children: [lightLogo && _jsx("img", { src: lightLogo, alt: 'logo', className: 'h-15 block dark:hidden' }), darkLogo && _jsx("img", { src: darkLogo, alt: 'logo', className: 'h-15 hidden dark:block' }), signupData && (_jsxs("div", { className: "my-6", children: ["Need to make a change?", " ", _jsx(Button, { onClick: goToSignup, children: " Go back" })] })), _jsx("div", { className: "flex flex-col space-y-2", children: collectSignupData && !localStorage.getItem('tenantName') ? (_jsx(SignupForm, { onSignup: onSignup, goBack: goBack })) : (_jsxs("div", { className: "flex flex-col", children: [_jsx("div", { className: "my-4", children: _jsx("h2", { className: "text-2xl font-bold text-center", children: "Log in or Sign up" }) }), _jsxs("div", { className: "max-w-2xl text-center my-2 px-2", children: ["First time here? No problem, it's free to try!", _jsx("br", {}), "We'll just ask you a couple of questions next and you'll be on your way."] }), _jsxs("div", { className: "flex items-center flex-col", children: [_jsxs("div", { className: "py-4 w-70", children: [_jsx(GoogleSignInButton, {}), _jsx(GitHubSignInButton, {}), _jsx(MicrosoftSignInButton, {})] }), _jsxs("div", { className: "flex items-center flex-row w-70 text-muted", children: [_jsx("hr", { className: "w-full" }), _jsx("div", { className: "px-2 text-xs", children: "OR" }), _jsx("hr", { className: "w-full" })] }), _jsx("div", { className: "py-4 w-70", children: _jsx(EnterpriseSigninButton, {}) })] }), authError && (_jsx("div", { className: "text-center", children: _jsxs("div", { className: "", children: ["Sorry, we have not been able to sign you in.", _jsx("br", {}), "Please try again or contact", _jsx("a", { className: 'text-info mx-1', href: "mailto:support@vertesiahq.com", children: "support@vertesiahq.com" }), "if it persists.", _jsxs("pre", { className: "mt-2", children: ["Error: ", authError.message] })] }) }))] })) })] }));
}
//# sourceMappingURL=SigninScreen.js.map