import { useLingui } from '../../../../../../../../node_modules/@lingui/react/dist/index.mjs';
import React__default, { useState } from 'react';
import { EMAIL_REGEX } from '../../../../../utils/index.mjs';
import { getTranslatedError } from '../../../../../utils/getTranslatedError.mjs';
import { readB2CInternals } from '../../../../../utils/internal.mjs';
import Button from '../../../../components/atoms/Button.mjs';
import Column from '../../../../components/atoms/Column.mjs';
import EmailInput from '../../../../components/molecules/EmailInput.mjs';
import { useGlobalReducer, useConfig, useStytch, useErrorCallback, AppScreens } from '../../../GlobalContextProvider.mjs';
import { hasProduct } from '../../../utils.mjs';
import { EmailSentType } from '../../../../../../../core/src/Events.mjs';

const PasswordsEmailForm = ()=>{
    const { i18n: $__i18n, _: $__ } = useLingui();
    const [, dispatch] = useGlobalReducer();
    const config = useConfig();
    const stytch = useStytch();
    const onError = useErrorCallback();
    const hasPasskeys = hasProduct(config.products, 'passkeys');
    const [email, setEmail] = useState('');
    const [errorMessage, setErrorMessage] = useState('');
    const [isSubmitting, setIsSubmitting] = useState(false);
    const handleSubmit = async (e)=>{
        e.preventDefault();
        if (!email.match(EMAIL_REGEX)) {
            setErrorMessage($__i18n._({
                id: "error.invalidEmail",
                message: "Email format is invalid."
            }));
        } else {
            setErrorMessage('');
            setIsSubmitting(true);
            readB2CInternals(stytch).searchManager.searchUser(email).then(({ userType })=>{
                dispatch({
                    type: 'update_password_state',
                    passwordState: {
                        email,
                        type: userType
                    }
                });
                dispatch({
                    type: 'transition',
                    screen: AppScreens.PasswordCreateOrLogin
                });
                readB2CInternals(stytch).networkClient.logEvent({
                    name: 'email_sent',
                    details: {
                        email: email,
                        type: EmailSentType.ResetPassword
                    }
                });
            }).catch((e)=>{
                onError(e);
                setIsSubmitting(false);
                setErrorMessage(getTranslatedError(e, $__));
            });
        }
    };
    return /*#__PURE__*/ React__default.createElement(Column, {
        as: "form",
        onSubmit: handleSubmit,
        gap: 4
    }, /*#__PURE__*/ React__default.createElement(EmailInput, {
        email: email,
        setEmail: setEmail,
        hasPasskeys: hasPasskeys,
        hideLabel: true,
        error: errorMessage
    }), /*#__PURE__*/ React__default.createElement(Button, {
        variant: "primary",
        loading: isSubmitting,
        type: "submit"
    }, $__i18n._({
        id: "button.emailLogin",
        message: "Continue with email"
    })));
};

export { PasswordsEmailForm };
//# sourceMappingURL=PasswordForm.mjs.map
