import { useLingui } from '../../../../../../../node_modules/@lingui/react/dist/index.mjs';
import React__default from 'react';
import { isSolanaWallet, signMessageWithWallet } from '../../../../utils/crypto.mjs';
import Button from '../../../components/atoms/Button.mjs';
import Column from '../../../components/atoms/Column.mjs';
import TextColumn from '../../../components/molecules/TextColumn.mjs';
import { useMountEffect } from '../../../hooks/useMountEffect.mjs';
import { useEventCallback, useErrorCallback, useStytch, useConfig, useGlobalReducer, AppScreens } from '../../GlobalContextProvider.mjs';
import { DEFAULT_SESSION_DURATION_MINUTES } from '../../../../../../core/src/constants.mjs';
import { StytchEventType } from '../../../../../../core/src/public/ui.mjs';

const SignMessage = ()=>{
    const { i18n: $__i18n, _: $__ } = useLingui();
    const onEvent = useEventCallback();
    const onError = useErrorCallback();
    const handleError = ()=>{
        // eslint-disable-next-line lingui/no-unlocalized-strings -- This is not shown in the UI, just sent to the error callback
        onError({
            message: '[Error] Could not sign message'
        });
        dispatch({
            type: 'transition',
            screen: AppScreens.CryptoError
        });
    };
    const stytchClient = useStytch();
    const config = useConfig();
    const [state, dispatch] = useGlobalReducer();
    const { walletChallenge, walletAddress, walletOption } = state.formState.cryptoState;
    const walletType = isSolanaWallet(walletOption) ? 'solana' : 'ethereum';
    const onSignWallet = async ()=>{
        const signature = await signMessageWithWallet({
            wallet: walletOption,
            message: walletChallenge,
            address: walletAddress
        });
        stytchClient.cryptoWallets.authenticate({
            crypto_wallet_address: walletAddress,
            crypto_wallet_type: walletType,
            signature,
            session_duration_minutes: config.sessionOptions?.sessionDurationMinutes ?? DEFAULT_SESSION_DURATION_MINUTES
        }).then((data)=>{
            onEvent({
                type: StytchEventType.CryptoWalletAuthenticate,
                data
            });
            dispatch({
                type: 'transition',
                screen: AppScreens.CryptoConfirmation
            });
        }).catch(handleError);
    };
    useMountEffect(()=>{
        onSignWallet();
    });
    return /*#__PURE__*/ React__default.createElement(Column, {
        gap: 6
    }, /*#__PURE__*/ React__default.createElement(TextColumn, {
        header: $__i18n._({
            id: "crypto.signMessage.title",
            message: "Completing signature request..."
        }),
        body: $__i18n._({
            id: "crypto.signMessage.instruction",
            message: "Complete the signature request through the wallet pop up in the right corner of your screen."
        }),
        helper: $__i18n._({
            id: "crypto.troubleHelp",
            message: "If you are having trouble, go back to the main screen and try logging in again."
        })
    }), /*#__PURE__*/ React__default.createElement(Button, {
        variant: "outline",
        onClick: ()=>dispatch({
                type: 'transition',
                screen: AppScreens.Main
            })
    }, $__i18n._({
        id: "button.goBack",
        message: "Go back"
    })));
};

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