import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { ContentLayout } from "../../Components/ContentLayout";
import { Form, Input } from "antd";
import { Link, useLocation } from "react-router-dom";
import { NextButton } from "./NextButton";
import { UnsuccessfulLoginModal } from "../Auth/UI-Components/UnsuccessfulLoginModal";
import { useActions, useAppState } from "../../overmind";
import { useCallback, useEffect, useState } from "react";
import { validUrl } from "../../utils/urlHelpers";
import isEmpty from "lodash/isEmpty";
export const LinkHash = () => {
    const queryString = useLocation().search;
    const actions = useActions();
    const state = useAppState();
    const [isErrorSubmit, setIsErrorSubmit] = useState(false);
    const done = () => {
        const code = state.flows.user.create.form.inviteHash;
        try {
            if (code === "LONDONX" || code === "LONDON22") {
                actions.flows.user.create.bypassInviteFlow({ couponCode: code });
                return;
            }
            actions.flows.user.create.verifyHash({ inviteHash: code || "" });
        }
        catch (e) {
            setIsErrorSubmit(true);
        }
    };
    useEffect(() => {
        actions.api.auth.logout();
    }, []);
    useEffect(() => {
        const urlParams = new URLSearchParams(queryString);
        actions.flows.user.create.stopMetamaskFlow();
        if (!isEmpty(urlParams.get("invite"))) {
            actions.flows.user.create.updateForm({ inviteHash: urlParams.get("invite") });
            done();
            actions.routing.historyPush({ location: "/signup/auth" });
        }
    }, [actions.flows.user, actions.routing, done, queryString]);
    const onChangeHandler = useCallback((e) => {
        let val = e.target.value;
        if (validUrl(e.target.value)) {
            const params = new URL(e.target.value).searchParams;
            val = params.get("invite") || e.target.value;
        }
        actions.flows.user.create.updateForm({ inviteHash: val });
    }, [actions]);
    return (_jsxs(ContentLayout, { children: [_jsx("div", { className: "nl-onboarding-title", children: "Enter your invite key" }), _jsx(Form, { onFinish: done, className: "nl-onboarding-form", children: _jsx(Input, { name: "hash", className: "nl-onboarding-input nl-onboarding-input-hash ant-form ant-form-item", onChange: onChangeHandler, defaultValue: state.flows.user.create.form.inviteHash, style: { textAlign: "center" } }) }), _jsx(NextButton, { isErrorSubmit: isErrorSubmit, nextProps: { text: "Next", command: () => done() }, visible: !isEmpty(state.flows.user.create.form.inviteHash), contentDescription: !state.flows.user.create.legacyToken && !state.auth.authenticated ? (_jsxs("div", { children: ["Enter hash or nft credentials. Or", _jsxs(Link, { to: "/auth/newlife-members", className: "paragraph-2u nl-onboarding-link", children: [" ", "click here", " "] }), "if you are a member from the Newlife mobile app."] })) : null }), _jsx(UnsuccessfulLoginModal, { redirect: actions.routing.historyPush })] }));
};
//# sourceMappingURL=LinkHash.js.map