import { useLingui } from '../../../../../../node_modules/@lingui/react/dist/index.mjs';
import React__default from 'react';
import { EMAIL_REGEX } from '../../../utils/index.mjs';
import { getTranslatedError } from '../../../utils/getTranslatedError.mjs';
import { readB2BInternals } from '../../../utils/internal.mjs';
import Button from '../../components/atoms/Button.mjs';
import Column from '../../components/atoms/Column.mjs';
import Typography from '../../components/atoms/Typography.mjs';
import ButtonColumn from '../../components/molecules/ButtonColumn.mjs';
import EmailInput from '../../components/molecules/EmailInput.mjs';
import { useGlobalReducer, useConfig, useEventCallback } from '../GlobalContextProvider.mjs';
import { AppScreens } from '../types/AppScreens.mjs';
import { usePasswordInput } from '../usePasswordInput.mjs';
import { StytchEventType } from '../../../../../core/src/public/ui.mjs';

const PasswordsForgotForm = ()=>{
    const [, dispatch] = useGlobalReducer();
    const config = useConfig();
    const onEvent = useEventCallback();
    const { i18n: $__i18n, _: $__ } = useLingui();
    const { stytch, email, setEmail, organization, errorMessage, setErrorMessage, isSubmitting, setIsSubmitting, handleNonMemberReset } = usePasswordInput();
    const handleSubmit = async (e)=>{
        e.preventDefault();
        if (!email.match(EMAIL_REGEX)) {
            setErrorMessage($__i18n._({
                id: "error.invalidEmailAddress",
                message: "Invalid email address"
            }));
            return;
        }
        setErrorMessage('');
        setIsSubmitting(true);
        if (organization) {
            readB2BInternals(stytch).searchManager.searchMember(email, organization.organization_id).then(({ member })=>{
                if (!member) {
                    handleNonMemberReset();
                    return;
                }
                // Member password flow: call reset password start instead
                stytch.passwords.resetByEmailStart({
                    email_address: email,
                    organization_id: organization.organization_id,
                    login_redirect_url: config.emailMagicLinksOptions?.loginRedirectURL,
                    reset_password_redirect_url: config.passwordOptions?.resetPasswordRedirectURL,
                    reset_password_expiration_minutes: config.passwordOptions?.resetPasswordExpirationMinutes,
                    reset_password_template_id: config.passwordOptions?.resetPasswordTemplateId,
                    verify_email_template_id: config.passwordOptions?.verifyEmailTemplateId,
                    locale: config.passwordOptions?.locale
                }).then((data)=>{
                    setIsSubmitting(false);
                    onEvent({
                        type: StytchEventType.B2BPasswordResetByEmailStart,
                        data
                    });
                    dispatch({
                        type: 'set_password_state',
                        email: email
                    });
                    dispatch({
                        type: 'transition',
                        screen: AppScreens.PasswordSetNewConfirmation
                    });
                }).catch((err)=>{
                    setIsSubmitting(false);
                    setErrorMessage(getTranslatedError(err, $__));
                });
            }).catch((err)=>{
                setIsSubmitting(false);
                setErrorMessage(getTranslatedError(err, $__));
            });
        } else {
            // Discovery password flow: call discovery reset password start instead. This flow doesn't need a member object
            stytch.passwords.discovery.resetByEmailStart({
                email_address: email,
                discovery_redirect_url: config.passwordOptions?.discoveryRedirectURL,
                reset_password_redirect_url: config.passwordOptions?.resetPasswordRedirectURL,
                reset_password_expiration_minutes: config.passwordOptions?.resetPasswordExpirationMinutes,
                reset_password_template_id: config.passwordOptions?.resetPasswordTemplateId,
                verify_email_template_id: config.passwordOptions?.verifyEmailTemplateId,
                locale: config.passwordOptions?.locale
            }).then(()=>{
                setIsSubmitting(false);
                dispatch({
                    type: 'set_password_state',
                    email: email
                });
                dispatch({
                    type: 'transition',
                    screen: AppScreens.PasswordSetNewConfirmation
                });
            }).catch((err)=>{
                setIsSubmitting(false);
                setErrorMessage(getTranslatedError(err, $__));
            });
        }
    };
    return /*#__PURE__*/ React__default.createElement(Column, {
        as: "form",
        gap: 6,
        onSubmit: handleSubmit
    }, /*#__PURE__*/ React__default.createElement(Typography, {
        variant: "header"
    }, $__i18n._({
        id: "password.forgot.title",
        message: "Check your email for help signing in"
    })), /*#__PURE__*/ React__default.createElement(Typography, {
        variant: "body"
    }, !organization ? $__i18n._({
        id: "password.forgot.content.discovery",
        message: "We'll email you a verification link to sign up for an account or reset your password if you have one."
    }) : $__i18n._({
        id: "password.forgot.content.organization",
        message: "We'll email you a login link to sign in to your account directly or reset your password if you have one."
    })), /*#__PURE__*/ React__default.createElement(EmailInput, {
        email: email,
        setEmail: setEmail,
        error: errorMessage
    }), /*#__PURE__*/ React__default.createElement(ButtonColumn, null, /*#__PURE__*/ React__default.createElement(Button, {
        variant: "primary",
        loading: isSubmitting,
        type: "submit"
    }, $__i18n._({
        id: "button.continue",
        message: "Continue"
    })), /*#__PURE__*/ React__default.createElement(Button, {
        variant: "ghost",
        onClick: ()=>dispatch({
                type: 'transition',
                screen: AppScreens.Main
            })
    }, $__i18n._({
        id: "button.goBack",
        message: "Go back"
    }))));
};

export { PasswordsForgotForm };
//# sourceMappingURL=PasswordForgotForm.mjs.map
