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 { readB2BInternals } 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 { PasswordInput } from '../../components/molecules/PasswordInput.mjs';
import { useGlobalReducer } from '../GlobalContextProvider.mjs';
import { AppScreens } from '../types/AppScreens.mjs';
import { usePasswordInput } from '../usePasswordInput.mjs';

const PasswordsEmailForm = ()=>{
    const [, dispatch] = useGlobalReducer();
    const { i18n: $__i18n, _: $__ } = useLingui();
    const { stytch, email, setEmail, password, setPassword, organization, errorMessage, setErrorMessage, isSubmitting, setIsSubmitting, submitPassword, submitDiscoveryPassword, handleNonMemberReset } = usePasswordInput();
    const [hideInput, setHideInput] = useState(!!organization && !email);
    const handleSubmit = async (e)=>{
        e.preventDefault();
        if (organization) {
            const organization_id = organization.organization_id;
            // If we have processed the email, submit the password field
            if (!hideInput) {
                submitPassword(organization_id);
                return;
            }
            // Validate we have a member with a password. Otherwise, check if we can jit
            // provision and kickoff verification and session reset
            if (!email.match(EMAIL_REGEX)) {
                setErrorMessage($__i18n._({
                    id: "error.invalidEmailAddress",
                    message: "Invalid email address"
                }));
                return;
            }
            setErrorMessage('');
            setIsSubmitting(true);
            readB2BInternals(stytch).searchManager.searchMember(email, organization.organization_id).then(({ member })=>{
                if (member) {
                    setHideInput(false);
                    setIsSubmitting(false);
                    dispatch({
                        type: 'set_password_state',
                        email
                    });
                    return;
                }
                // No member password flow: verify email and use reset by session
                handleNonMemberReset();
            }).catch((err)=>{
                setIsSubmitting(false);
                setErrorMessage(getTranslatedError(err, $__));
            });
        } else {
            // Discovery flow
            submitDiscoveryPassword();
        }
    };
    const onGetHelp = ()=>{
        dispatch({
            type: 'set_password_state',
            email: email
        });
        dispatch({
            type: 'transition',
            screen: AppScreens.PasswordForgotForm
        });
    };
    return /*#__PURE__*/ React__default.createElement(Column, {
        as: "form",
        onSubmit: handleSubmit,
        gap: 2
    }, /*#__PURE__*/ React__default.createElement(EmailInput, {
        email: email,
        setEmail: setEmail
    }), !hideInput && /*#__PURE__*/ React__default.createElement(PasswordInput, {
        password: password,
        setPassword: setPassword,
        type: "current",
        error: errorMessage
    }), /*#__PURE__*/ React__default.createElement(Button, {
        variant: "primary",
        loading: isSubmitting,
        type: "submit"
    }, $__i18n._({
        id: "button.continue",
        message: "Continue"
    })), !hideInput && /*#__PURE__*/ React__default.createElement(Button, {
        variant: "outline",
        onClick: onGetHelp
    }, $__i18n._({
        id: "link.signupResetPassword",
        message: "Sign up or reset password"
    })));
};

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