import { isEmpty } from "lodash";
import { statemachine } from "overmind";
export const routesToCreateWizard = {
    "/": "HASH_VERIFY",
    "/signup/auth": "AUTHENTICATE",
    "/signup/domain": "SELECT_DOMAIN",
    "/signup/create": "CREATE_USER",
    "/signup/subscribe": "SUBSCRIBE",
};
// const forwAuthorized = (authorized: boolean, step: States): States => authorized ? { current: 'DONE', hasNext: false, hasPrev: false } : step;
// const forwAuthenticated = (authenticated: boolean, step: States): States => authenticated ? { current: 'CREATE_USER', hasNext: false, hasPrev: true } : step;
const defaults = {
    HASH_VERIFY: (d) => ({
        current: "HASH_VERIFY",
        hasNext: !!d?.hasNext,
        hasPrev: false,
        nextLink: d?.link,
    }),
    AUTHENTICATE: (d) => ({
        current: "AUTHENTICATE",
        hasNext: false,
        hasPrev: true,
        nextLink: d?.link,
    }),
    SELECT_DOMAIN: (d) => ({
        current: "SELECT_DOMAIN",
        hasNext: false,
        hasPrev: false,
        nextLink: d?.link,
    }),
    DONE: () => ({ current: "DONE", hasNext: false, hasPrev: false }),
    CREATE_USER: (d) => ({
        current: "CREATE_USER",
        hasNext: !!d?.hasNext,
        hasPrev: true,
        nextLink: d?.link,
    }),
    SUBSCRIBE: (d) => ({ current: "SUBSCRIBE", hasNext: true, hasPrev: true, nextLink: "/signup/create" }),
};
// export type WizardMachine = statemachine<States, Events, BaseState>;
export const Wizard = statemachine({
    HASH_VERIFY: {
        NEXT: ({ inviteHash, inviteHashVerified }) => inviteHash && inviteHashVerified ? defaults.AUTHENTICATE() : defaults.HASH_VERIFY(),
        UPDATE: ({ inviteHash, isLegacyUpdateOngoing, inviteHashVerified, metamaskFlow }) => {
            return metamaskFlow
                ? defaults.AUTHENTICATE()
                : isLegacyUpdateOngoing
                    ? defaults.SELECT_DOMAIN()
                    : {
                        ...defaults.HASH_VERIFY(),
                        hasNext: !isEmpty(inviteHash),
                        nextLink: isEmpty(inviteHash) && !inviteHashVerified ? undefined : "/signup/auth",
                    };
        },
    },
    AUTHENTICATE: {
        NEXT: ({ legacyToken, authenticated, authorized, user }) => {
            if (isEmpty(legacyToken)) {
                if (!authorized || (!authenticated && user?.status !== "invited")) {
                    return defaults.AUTHENTICATE();
                }
                return defaults.SELECT_DOMAIN();
            }
            return defaults.CREATE_USER();
        },
        UPDATE: ({ authorized, authenticated, isLegacyUpdateOngoing, user, metamaskFlow, inviteHashVerified }) => {
            return (authorized && !isLegacyUpdateOngoing && user?.status !== "invited"
                ? defaults.DONE()
                : authenticated && !user?.subscriptionStatus?.startsWith("io-domain-sale")
                    ? defaults.SELECT_DOMAIN()
                    : !metamaskFlow && !inviteHashVerified && !isLegacyUpdateOngoing
                        ? defaults.HASH_VERIFY()
                        : {
                            ...defaults.AUTHENTICATE(),
                            nextLink: isLegacyUpdateOngoing || user?.subscriptionStatus?.startsWith("io-domain-sale")
                                ? "/signup/create"
                                : "/signup/domain",
                        });
        },
    },
    SELECT_DOMAIN: {
        NEXT: ({ authenticated, subscribed, isLegacyUpdateOngoing, formUsername, authorized, metamaskFlow }) => {
            return authenticated
                ? metamaskFlow
                    ? defaults.CREATE_USER()
                    : subscribed || isLegacyUpdateOngoing || formUsername.replace(/\.io$/, "").length > 5
                        ? defaults.CREATE_USER()
                        : defaults.SUBSCRIBE()
                : authorized
                    ? defaults.DONE()
                    : defaults.AUTHENTICATE();
        },
        UPDATE: ({ authorized, authenticated, formUsername, formUsernameIsAvailable, user, featureFlags, subscribed, isLegacyUpdateOngoing, metamaskFlow, }) => {
            return authorized && !["imported"].includes(user?.status || "") && !isLegacyUpdateOngoing
                ? defaults.DONE()
                : (!isLegacyUpdateOngoing && authenticated && subscribed) || (authenticated && metamaskFlow)
                    ? defaults.CREATE_USER()
                    : {
                        ...defaults.SELECT_DOMAIN(),
                        hasNext: 
                        // (formUsername.length === 12) &&
                        (featureFlags.onboarding.premiumDomains || formUsername.replace(/\.io$/, "").length > 5) &&
                            formUsernameIsAvailable === "available",
                        nextLink: isLegacyUpdateOngoing || !(authorized || authenticated)
                            ? "/signup/auth"
                            : subscribed || isLegacyUpdateOngoing || formUsername.replace(/\.io$/, "").length > 5 || metamaskFlow
                                ? "/signup/create"
                                : "/signup/subscribe",
                    };
        },
    },
    SUBSCRIBE: {
        NEXT: ({ subscribed }) => (subscribed ? defaults.CREATE_USER() : defaults.SUBSCRIBE()),
        PREV: ({ formUsername }) => ({ ...defaults.SELECT_DOMAIN(), hasNext: !!formUsername }),
        UPDATE: ({ authorized, authenticated, subscribed, user, formUsername, featureFlags, couponCode }) => {
            return (authorized && authenticated
                ? defaults.DONE()
                : subscribed ||
                    (user?.id && (!featureFlags.onboarding.premiumDomains || formUsername.replace(/\.io$/, "").length > 5)) ||
                    couponCode === "NEWFORUM-100-DROP1307"
                    ? defaults.CREATE_USER()
                    : defaults.SUBSCRIBE());
        },
    },
    CREATE_USER: {
        NEXT: () => defaults.DONE(),
        PREV: ({ authenticated, formUsername }) => {
            return (authenticated ? { ...defaults.SELECT_DOMAIN(), hasNext: !!formUsername } : { ...defaults.AUTHENTICATE(), hasNext: false });
        },
        UPDATE: ({ authorized, authenticated, inviteHashVerified, user, formUsername, isLegacyUpdateOngoing }) => {
            return !isLegacyUpdateOngoing && (!inviteHashVerified || !authenticated)
                ? defaults.HASH_VERIFY()
                : authorized && ["registered", "admitted"].includes(user?.status || "") && !isLegacyUpdateOngoing
                    ? defaults.DONE()
                    : defaults.CREATE_USER({ hasNext: !!formUsername, link: "/" });
        },
    },
    DONE: {
        UPDATE: ({ authorized }) => {
            return authorized ? defaults.DONE() : defaults.SELECT_DOMAIN();
        },
    },
});
//# sourceMappingURL=wizardStateMachine.js.map