import { useLingui } from '../../../../../../node_modules/@lingui/react/dist/index.mjs';
import React__default from 'react';
import { EMAIL_REGEX } from '../../../utils/index.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 { PasswordInput } from '../../components/molecules/PasswordInput.mjs';
import { useGlobalReducer } from '../GlobalContextProvider.mjs';
import { AppScreens } from '../types/AppScreens.mjs';
import { usePasswordInput } from '../usePasswordInput.mjs';

const PasswordsAuthenticate = ()=>{
    const [, dispatch] = useGlobalReducer();
    const { i18n: $__i18n, _: $__ } = useLingui();
    const { email, setEmail, password, setPassword, organization, errorMessage, setErrorMessage, isSubmitting, submitPassword, submitDiscoveryPassword } = usePasswordInput();
    const handleSubmit = async (e)=>{
        e.preventDefault();
        if (!email.match(EMAIL_REGEX)) {
            setErrorMessage($__i18n._({
                id: "error.invalidEmailAddress",
                message: "Invalid email address"
            }));
        } else {
            if (!organization) {
                submitDiscoveryPassword();
            } else {
                submitPassword(organization.organization_id);
            }
        }
    };
    const onGetHelp = ()=>{
        dispatch({
            type: 'set_password_state',
            email: email
        });
        dispatch({
            type: 'transition',
            screen: AppScreens.PasswordForgotForm
        });
    };
    return /*#__PURE__*/ React__default.createElement(Column, {
        gap: 6
    }, /*#__PURE__*/ React__default.createElement(Typography, {
        variant: "header"
    }, $__i18n._({
        id: "password.login.title",
        message: "Log in with email and password"
    })), /*#__PURE__*/ React__default.createElement(Column, {
        as: "form",
        onSubmit: handleSubmit,
        gap: 4
    }, /*#__PURE__*/ React__default.createElement(EmailInput, {
        email: email,
        setEmail: setEmail
    }), /*#__PURE__*/ React__default.createElement(PasswordInput, {
        password: password,
        setPassword: setPassword,
        type: "current",
        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: "outline",
        onClick: onGetHelp
    }, $__i18n._({
        id: "link.signupResetPassword",
        message: "Sign up or reset password"
    })), /*#__PURE__*/ React__default.createElement(Button, {
        variant: "ghost",
        onClick: ()=>dispatch({
                type: 'transition',
                screen: AppScreens.Main
            })
    }, $__i18n._({
        id: "button.goBack",
        message: "Go back"
    })))));
};

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