import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import { useState } from 'react';
import { Select, TextInput } from '@inkjs/ui';
import { isHex } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
export const ChooseExecutionOption = ({ onSubmit, }) => {
    const [chosePrivateKey, setChosePrivateKey] = useState(false);
    return (_jsxs(Box, { flexDirection: "column", gap: 1, children: [_jsxs(Box, { gap: 1, children: [_jsx(Text, { bold: true, children: "\uD83D\uDE80 Ready to deploy!" }), _jsx(Text, { bold: true, children: "How would you like to deploy?" })] }), _jsx(Select, { options: [
                    { label: '🔑 Enter a private key', value: 'privateKey' },
                    {
                        label: '🔌 Connect a wallet (Metamask / WalletConnect)',
                        value: 'externalSigner',
                    },
                ], onChange: option => {
                    if (option === 'privateKey') {
                        setChosePrivateKey(true);
                    }
                    else if (option === 'externalSigner') {
                        onSubmit({ type: 'externalSigner' });
                    }
                } }), chosePrivateKey && (_jsx(PrivateKeyInput, { onSubmit: privateKey => {
                    onSubmit({ type: 'privateKey', privateKey });
                } }))] }));
};
const PrivateKeyInput = ({ onSubmit }) => {
    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;
                    }
                    try {
                        privateKeyToAccount(privateKey);
                    }
                    catch (err) {
                        const error = err;
                        setErrorMessage(
                        // @ts-expect-error
                        `Invalid private key: ${error.shortMessage ?? error.message}`);
                        setResetKey(prev => prev + 1);
                        return;
                    }
                    onSubmit(privateKey);
                } }, resetKey), errorMessage && (_jsx(Box, { children: _jsx(Text, { color: "red", children: errorMessage ? `❌ ${errorMessage}` : ' ' }) }))] }));
};
