import { useLingui } from '../../../../../../../node_modules/@lingui/react/dist/index.mjs';
import wn from '../../../../../../../node_modules/preact/compat/dist/compat.module.mjs';
import { convertPasswordResetOptions } from '../../../../utils/index.mjs';
import { getTranslatedError } from '../../../../utils/getTranslatedError.mjs';
import Button from '../../../components/atoms/Button.mjs';
import Column from '../../../components/atoms/Column.mjs';
import ButtonColumn from '../../../components/molecules/ButtonColumn.mjs';
import EmailInput from '../../../components/molecules/EmailInput.mjs';
import { PasswordInput } from '../../../components/molecules/PasswordInput.mjs';
import { useStytch, useConfig, useGlobalReducer, useEventCallback, useErrorCallback, AppScreens } from '../../GlobalContextProvider.mjs';
import { useState as h } from '../../../../../../../node_modules/preact/hooks/dist/hooks.module.mjs';
import { DEFAULT_SESSION_DURATION_MINUTES } from '../../../../../../core/src/constants.mjs';
import { StytchEventType } from '../../../../../../core/src/public/ui.mjs';

const PasswordAuthenticate = ()=>{
    const { i18n: $__i18n, _: $__ } = useLingui();
    const stytchClient = useStytch();
    const config = useConfig();
    const passwordOptions = config.passwordOptions;
    const [state, dispatch] = useGlobalReducer();
    const onEvent = useEventCallback();
    const onError = useErrorCallback();
    const [email, setEmail] = h(state.formState.passwordState.email);
    const [password, setPassword] = h('');
    const [errorMessage, setErrorMessage] = h('');
    const [isSubmitting, setIsSubmitting] = h(false);
    const handleSubmit = (e)=>{
        e.preventDefault();
        setErrorMessage('');
        setIsSubmitting(true);
        stytchClient.passwords.authenticate({
            email,
            password,
            session_duration_minutes: config.sessionOptions?.sessionDurationMinutes ?? DEFAULT_SESSION_DURATION_MINUTES
        }).then((data)=>{
            setIsSubmitting(false);
            onEvent({
                type: StytchEventType.PasswordAuthenticate,
                data
            });
            dispatch({
                type: 'transition',
                screen: AppScreens.PasswordConfirmation
            });
        }).catch((e)=>{
            onError(e);
            if (e.error_type === 'reset_password') {
                stytchClient.passwords.resetByEmailStart(convertPasswordResetOptions(email, passwordOptions)).then(()=>{
                    dispatch({
                        type: 'transition',
                        screen: AppScreens.PasswordDedupe
                    });
                });
            } else {
                setErrorMessage(getTranslatedError(e, $__));
            }
            setIsSubmitting(false);
        });
    };
    const onForgot = ()=>{
        stytchClient.passwords.resetByEmailStart(convertPasswordResetOptions(email, passwordOptions)).then(()=>{
            dispatch({
                type: 'transition',
                screen: AppScreens.PasswordForgot
            });
        });
    };
    return /*#__PURE__*/ wn.createElement(Column, {
        as: "form",
        onSubmit: handleSubmit,
        gap: 4
    }, /*#__PURE__*/ wn.createElement(EmailInput, {
        email: email,
        setEmail: setEmail,
        disabled: true
    }), /*#__PURE__*/ wn.createElement(PasswordInput, {
        password: password,
        setPassword: setPassword,
        type: "current",
        error: errorMessage
    }), /*#__PURE__*/ wn.createElement(ButtonColumn, null, /*#__PURE__*/ wn.createElement(Button, {
        variant: "primary",
        loading: isSubmitting,
        type: "submit"
    }, $__i18n._({
        id: "button.continue",
        message: "Continue"
    })), /*#__PURE__*/ wn.createElement(Button, {
        variant: "outline",
        onClick: onForgot
    }, $__i18n._({
        id: "button.forgotPassword",
        message: "Forgot your password?"
    }))));
};

export { PasswordAuthenticate };
//# sourceMappingURL=PasswordAuthenticate.mjs.map
