import React__default, { useState, useEffect } from 'react';
import { readB2CInternals } from '../../../../utils/internal.mjs';
import { useCountdown } from '../../../components/atoms/Countdown.mjs';
import { EmailConfirmationView } from '../../../components/organisms/EmailConfirmationView.mjs';
import { useStytch, useGlobalReducer, useConfig, AppScreens } from '../../GlobalContextProvider.mjs';
import { usePasswordlessAuthenticate } from './usePasswordlessAuthenticate.mjs';
import { EmailSentType } from '../../../../../../core/src/Events.mjs';

/**
 * Resolves the best email domain to use as a hint for login
 */ const useEmailDomain = ()=>{
    const stytch = useStytch();
    const config = useConfig();
    /**
   * Read the emailDomains out of sync storage, then read out of async storage
   * TODO - Should all bootstrap data live in the config somewhere?
   */ const [emailDomains, setEmailDomains] = useState(()=>{
        const { emailDomains } = readB2CInternals(stytch).bootstrap.getSync();
        return emailDomains;
    });
    useEffect(()=>{
        readB2CInternals(stytch).bootstrap.getAsync().then(({ emailDomains })=>{
            setEmailDomains(emailDomains);
        });
    }, [
        stytch
    ]);
    // If a domain is explicitly passed in, use it!
    if (config.emailMagicLinksOptions?.domainHint) {
        return config.emailMagicLinksOptions?.domainHint;
    }
    // If there can only be one logical choice, use it!
    if (emailDomains?.length === 1) {
        return emailDomains[0];
    }
    // If it isn't clear who sent the email
    // we simply won't provide a hint!
    return null;
};
const EmailConfirmation = ({ showGoBack = true })=>{
    const stytchClient = useStytch();
    const [state, dispatch] = useGlobalReducer();
    const emailDomain = useEmailDomain();
    const countdown = useCountdown();
    const { sendLink, isSubmitting } = usePasswordlessAuthenticate();
    const email = state.formState.magicLinkState.email !== '' ? state.formState.magicLinkState.email : state.formState.passwordState.email;
    const goBack = ()=>{
        dispatch({
            type: 'set_magic_link_email',
            email: ''
        });
        dispatch({
            type: 'transition',
            screen: AppScreens.Main
        });
        readB2CInternals(stytchClient).networkClient.logEvent({
            name: 'email_try_again_clicked',
            details: {
                email: email,
                type: EmailSentType.LoginOrCreateEML
            }
        });
    };
    return /*#__PURE__*/ React__default.createElement(EmailConfirmationView, {
        emailDomain: emailDomain,
        email: email,
        goBack: showGoBack ? goBack : undefined,
        resend: sendLink,
        countdown: countdown,
        isSubmitting: isSubmitting
    });
};

export { EmailConfirmation, useEmailDomain };
//# sourceMappingURL=EmailConfirmation.mjs.map
