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 = ({ label, onSubmit, }) => {
    const [chosePrivateKey, setChosePrivateKey] = useState(false);
    const [choseSponsoredSender, setChoseSponsoredSender] = useState(false);
    return (_jsxs(Box, { flexDirection: "column", gap: 1, children: [_jsxs(Box, { gap: 1, children: [_jsx(Text, { bold: true, children: label }), _jsx(Text, { bold: true, children: "How would you like to send the transaction?" })] }), _jsx(Select, { options: [
                    { label: '🔑 Enter a private key', value: 'privateKey' },
                    {
                        label: '🔌 Connect a wallet (Metamask / WalletConnect)',
                        value: 'externalSigner',
                    },
                    {
                        label: '💰 EthDenver 2025 sponsored transactions (No gas fees required)',
                        value: 'sponsoredSender',
                    },
                ], onChange: option => {
                    if (option === 'privateKey') {
                        setChosePrivateKey(true);
                    }
                    else if (option === 'externalSigner') {
                        onSubmit({ type: 'externalSigner' });
                    }
                    else if (option === 'sponsoredSender') {
                        setChoseSponsoredSender(true);
                    }
                } }), chosePrivateKey && (_jsx(PrivateKeyInput, { onSubmit: privateKey => {
                    onSubmit({ type: 'privateKey', privateKey });
                } })), choseSponsoredSender && (_jsx(ApiKeyInput, { onSubmit: apiKey => {
                    onSubmit({ type: 'sponsoredSender', apiKey });
                } }))] }));
};
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}` : ' ' }) }))] }));
};
export const DEFAULT_API_KEY_ETH_DENVER_2025 = 'eth-denver-2025-shared-7947';
const ApiKeyInput = ({ 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 API key for the sponsored sender:" }) }), _jsx(TextInput, { defaultValue: DEFAULT_API_KEY_ETH_DENVER_2025, onSubmit: apiKey => {
                    if (!apiKey || apiKey.trim() === '') {
                        setErrorMessage('API key cannot be empty');
                        setResetKey(prev => prev + 1);
                        return;
                    }
                    onSubmit(apiKey.trim());
                } }, resetKey), errorMessage && (_jsx(Box, { children: _jsx(Text, { color: "red", children: errorMessage ? `❌ ${errorMessage}` : ' ' }) }))] }));
};
