import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text, useInput } from 'ink';
import Spinner from 'ink-spinner';
import { useEffect, useState } from 'react';
import { getColors } from '../config/index.js';
import { formatError } from '../utils/error-formatter.js';
/**
 * Generic device-flow OAuth login UI. Shared by /codex-login and
 * /copilot-login — supplies the provider-specific flow function and the
 * display name; the rest of the lifecycle (starting → visit → polling →
 * done/error) is identical for any device-flow OAuth provider.
 */
export function OAuthLogin({ providerName, displayName, runFlow, onDone, }) {
    const colors = getColors();
    const [status, setStatus] = useState('starting');
    const [verificationUrl, setVerificationUrl] = useState('');
    const [userCode, setUserCode] = useState('');
    const [error, setError] = useState(null);
    useEffect(() => {
        let cancelled = false;
        (async () => {
            try {
                await runFlow(providerName, {
                    onShowCode(url, code) {
                        if (cancelled)
                            return;
                        setVerificationUrl(url);
                        setUserCode(code);
                        setStatus('visit');
                    },
                    onPollingStart() {
                        if (!cancelled)
                            setStatus('polling');
                    },
                    delayBeforePollMs: 500,
                });
                if (!cancelled) {
                    setStatus('done');
                }
            }
            catch (err) {
                if (!cancelled) {
                    setError(formatError(err));
                    setStatus('error');
                }
            }
        })();
        return () => {
            cancelled = true;
        };
    }, [providerName, runFlow]);
    useInput((_input, key) => {
        if (key.escape) {
            onDone?.({ success: false, error: 'Login cancelled.' });
        }
        else if (key.return && status === 'done') {
            onDone?.({ success: true });
        }
        else if (key.return && status === 'error') {
            onDone?.({ success: false, error: error ?? undefined });
        }
    });
    if (status === 'starting') {
        return (_jsx(Box, { flexDirection: "column", paddingY: 1, children: _jsxs(Text, { color: colors.primary, children: [_jsx(Spinner, { type: "dots" }), " Starting ", displayName, " login\u2026"] }) }));
    }
    if (status === 'visit' || status === 'polling') {
        return (_jsxs(Box, { flexDirection: "column", paddingY: 1, children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { bold: true, children: "Visit this URL and enter the code:" }) }), _jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: colors.primary, children: verificationUrl }) }), _jsxs(Text, { bold: true, children: ["Code: ", userCode] }), status === 'polling' && (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: colors.primary, children: [_jsx(Spinner, { type: "dots" }), " Waiting for you to complete login\u2026"] }) }))] }));
    }
    if (status === 'done') {
        return (_jsxs(Box, { flexDirection: "column", paddingY: 1, children: [_jsx(Box, { marginBottom: 1, children: _jsxs(Text, { color: colors.success, children: ["Logged in. Credentials saved for \"", providerName, "\"."] }) }), _jsx(Text, { children: "Press Enter to continue." })] }));
    }
    if (status === 'error') {
        return (_jsxs(Box, { flexDirection: "column", paddingY: 1, children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: colors.error, children: error }) }), _jsx(Text, { children: "Press Enter to continue." })] }));
    }
    return null;
}
//# sourceMappingURL=oauth-login.js.map