import { useLingui } from '../../../../../../node_modules/@lingui/react/dist/index.mjs';
import React__default, { useState } from 'react';
import { extractErrorMessage } from '../../../utils/extractErrorMessage.mjs';
import Button from '../../components/atoms/Button.mjs';
import Column from '../../components/atoms/Column.mjs';
import Typography from '../../components/atoms/Typography.mjs';
import EmailInput from '../../components/molecules/EmailInput.mjs';
import { useGlobalReducer, useStytch, useConfig } from '../GlobalContextProvider.mjs';
import { useSsoDiscoveryConnection } from '../hooks/useSsoDiscoveryConnection.mjs';

const SSODiscoveryEmail = ()=>{
    const [, dispatch] = useGlobalReducer();
    const [email, setEmail] = useState('');
    const { i18n: $__i18n, _: $__ } = useLingui();
    const [error, setError] = useState();
    const [isLoading, setIsLoading] = useState(false);
    const { trigger } = useSsoDiscoveryConnection();
    const stytch = useStytch();
    const { ssoOptions } = useConfig();
    const handleSubmit = async (e)=>{
        e.preventDefault();
        setError(undefined);
        const tryGetConnections = async ()=>{
            try {
                const response = await trigger({
                    emailAddress: email
                });
                if (!response) {
                    return [];
                }
                return response.connections;
            } catch (e) {
                setError(extractErrorMessage(e, $__i18n._({
                    id: "ssoDiscovery.email.error.unknown",
                    message: "Something went wrong. Try again later or contact your admin for help."
                })));
            }
        };
        try {
            setIsLoading(true);
            const connections = await tryGetConnections();
            if (connections?.length === 1) {
                const [connection] = connections;
                await stytch.sso.start({
                    connection_id: connection.connection_id,
                    login_redirect_url: ssoOptions?.loginRedirectURL,
                    signup_redirect_url: ssoOptions?.signupRedirectURL
                });
                // We intentionally leave `isLoading` set to `true` since we're expecting
                // the user to be redirected
                return;
            }
            setIsLoading(false);
            if (connections) {
                dispatch({
                    type: 'set_sso_discovery_state',
                    connections
                });
            }
        } catch (e) {
            setIsLoading(false);
            throw e;
        }
    };
    const handleBack = ()=>{
        dispatch({
            type: 'navigate_back'
        });
    };
    return /*#__PURE__*/ React__default.createElement(Column, {
        gap: 6
    }, /*#__PURE__*/ React__default.createElement(Typography, {
        variant: "header"
    }, $__i18n._({
        id: "ssoDiscovery.email.title",
        message: "Enter your email to continue"
    })), /*#__PURE__*/ React__default.createElement(Column, {
        as: "form",
        onSubmit: handleSubmit,
        gap: 2
    }, /*#__PURE__*/ React__default.createElement(EmailInput, {
        email: email,
        setEmail: setEmail,
        hideLabel: true,
        error: error
    }), /*#__PURE__*/ React__default.createElement(Button, {
        variant: "primary",
        loading: isLoading,
        type: "submit"
    }, $__i18n._({
        id: "button.continue",
        message: "Continue"
    })), /*#__PURE__*/ React__default.createElement(Button, {
        variant: "ghost",
        onClick: handleBack
    }, $__i18n._({
        id: "button.goBack",
        message: "Go back"
    }))));
};

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