"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
/*
Create an anonymous account.
*/
const react_1 = require("react");
const antd_1 = require("antd");
const logo_square_1 = __importDefault(require("components/logo-square"));
const use_customize_1 = __importDefault(require("lib/use-customize"));
const shared_1 = require("./shared");
const A_1 = __importDefault(require("components/misc/A"));
const post_1 = __importDefault(require("lib/api/post"));
const misc_1 = require("@cocalc/util/misc");
const loading_1 = __importDefault(require("components/share/loading"));
const react_google_recaptcha_v3_1 = require("react-google-recaptcha-v3");
function Try(props) {
    const { reCaptchaKey } = (0, use_customize_1.default)();
    return ((0, jsx_runtime_1.jsx)(react_google_recaptcha_v3_1.GoogleReCaptchaProvider, { reCaptchaKey: reCaptchaKey, children: (0, jsx_runtime_1.jsx)(Try0, { ...props }) }));
}
exports.default = Try;
function Try0({ minimal, onSuccess }) {
    const { siteName, anonymousSignup, reCaptchaKey } = (0, use_customize_1.default)();
    const [state, setState] = (0, react_1.useState)("wait");
    const [error, setError] = (0, react_1.useState)("");
    const { executeRecaptcha } = (0, react_google_recaptcha_v3_1.useGoogleReCaptcha)();
    if (!anonymousSignup) {
        return ((0, jsx_runtime_1.jsxs)("h1", { style: { textAlign: "center", margin: "45px auto" }, children: ["Anonymous Trial of ", siteName, " Not Currently Available"] }));
    }
    async function createAnonymousAccount() {
        setState("creating");
        try {
            let reCaptchaToken;
            if (reCaptchaKey) {
                if (!executeRecaptcha) {
                    throw Error("Please wait a few seconds, then try again.");
                }
                reCaptchaToken = await executeRecaptcha("anonymous");
            }
            const result = await (0, post_1.default)("/auth/sign-up", { reCaptchaToken });
            if (result.issues && (0, misc_1.len)(result.issues) > 0) {
                throw Error(JSON.stringify(result.issues)); // TODO: should not happen, except for captcha error...
            }
            onSuccess?.();
        }
        catch (err) {
            setError(err.message);
            setState("wait");
        }
    }
    return ((0, jsx_runtime_1.jsxs)("div", { style: { margin: "30px", minHeight: "50vh" }, children: [!minimal && ((0, jsx_runtime_1.jsxs)("div", { style: { textAlign: "center", marginBottom: "15px" }, children: [(0, jsx_runtime_1.jsx)(logo_square_1.default, { style: { width: "100px", height: "100px", marginBottom: "15px" } }), (0, jsx_runtime_1.jsxs)("h1", { children: ["Use ", siteName, " Anonymously"] })] })), (0, jsx_runtime_1.jsxs)("div", { style: shared_1.LOGIN_STYLE, children: [error && (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: error, showIcon: true }), "Try ", siteName, " ", (0, jsx_runtime_1.jsx)("b", { children: "without" }), " ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/auth/sign-up", external: !!minimal, children: "creating an account" }), "!", (0, jsx_runtime_1.jsx)(antd_1.Button, { disabled: state != "wait", shape: "round", size: "large", type: "primary", style: { width: "100%", marginTop: "20px" }, onClick: createAnonymousAccount, children: state == "creating" ? ((0, jsx_runtime_1.jsx)(loading_1.default, { children: "Configuring Anonymous Access..." })) : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Use ", siteName, " Anonymously"] })) })] }), !minimal && ((0, jsx_runtime_1.jsxs)("div", { style: {
                    ...shared_1.LOGIN_STYLE,
                    backgroundColor: "white",
                    margin: "30px auto",
                    padding: "15px",
                }, children: ["Already have an account? ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/auth/sign-in", children: "Sign In" }), (0, jsx_runtime_1.jsxs)("div", { style: { marginTop: "15px" }, children: ["Need an account? ", (0, jsx_runtime_1.jsx)(A_1.default, { href: "/auth/sign-up", children: "Sign Up" })] })] }))] }));
}
//# sourceMappingURL=try.js.map