import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useBridgeWizardStore } from './bridgeWizardStore.js';
import { Box, Text } from 'ink';
import { TextInput } from '@inkjs/ui';
import { privateKeyToAccount } from 'viem/accounts';
import { useState } from 'react';
import { isHex } from 'viem';
export const EnterPrivateKey = () => {
    const { wizardState, submitEnterPrivateKey } = useBridgeWizardStore();
    if (wizardState.stepId !== 'enter-private-key') {
        throw new Error('Invalid state');
    }
    const [errorMessage, setErrorMessage] = useState('');
    const [resetKey, setResetKey] = useState(0);
    return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { children: _jsx(Text, { bold: true, children: "Enter your private key for your account:" }) }), _jsx(TextInput, { onSubmit: privateKey => {
                    if (!isHex(privateKey)) {
                        setErrorMessage('Invalid private key: must start with 0x');
                        setResetKey(prev => prev + 1);
                        return;
                    }
                    let account;
                    try {
                        account = privateKeyToAccount(privateKey);
                    }
                    catch (err) {
                        const error = err;
                        setErrorMessage(
                        // @ts-expect-error
                        `Invalid private key: ${error.shortMessage ?? error.message}`);
                        setResetKey(prev => prev + 1);
                        return;
                    }
                    submitEnterPrivateKey({
                        privateKey,
                        address: account.address,
                    });
                } }, resetKey), errorMessage && (_jsx(Box, { children: _jsx(Text, { color: "red", children: errorMessage ? `❌ ${errorMessage}` : ' ' }) }))] }));
};
