import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import React, { useEffect, useState } from 'react';
import { Box, Text } from 'ink';
import { SixthWelcomeScreen } from './SixthWelcomeScreen.js';
import { SixthAuthFlow } from '@google/gemini-cli-core';
import { theme } from '../semantic-colors.js';
var AuthState;
(function (AuthState) {
    AuthState["Checking"] = "checking";
    AuthState["NewUser"] = "newUser";
    AuthState["Authenticating"] = "authenticating";
    AuthState["Success"] = "success";
    AuthState["Error"] = "error";
})(AuthState || (AuthState = {}));
export const SixthAuthHandler = ({ onAuthComplete, onAuthError, }) => {
    const [authState, setAuthState] = useState(AuthState.Checking);
    const [authFlow] = useState(() => new SixthAuthFlow());
    const [error, setError] = useState('');
    useEffect(() => {
        checkAuthStatus();
        // Cleanup function
        return () => {
            authFlow.stop();
        };
    }, []);
    const checkAuthStatus = async () => {
        try {
            const isAuthenticated = await authFlow.isUserAuthenticated();
            if (isAuthenticated) {
                const userInfo = await authFlow.getUserInfo();
                if (userInfo) {
                    setAuthState(AuthState.Success);
                    onAuthComplete(userInfo);
                    return;
                }
            }
            // User is not authenticated, show welcome screen
            setAuthState(AuthState.NewUser);
        }
        catch (err) {
            const errorMessage = err instanceof Error ? err.message : 'Failed to check authentication status';
            setError(errorMessage);
            setAuthState(AuthState.Error);
            onAuthError(errorMessage);
        }
    };
    const handleContinueAuth = async () => {
        setAuthState(AuthState.Authenticating);
        try {
            const userInfo = await authFlow.startAuthFlow();
            setAuthState(AuthState.Success);
            onAuthComplete(userInfo);
        }
        catch (err) {
            const errorMessage = err instanceof Error ? err.message : 'Authentication failed';
            setError(errorMessage);
            setAuthState(AuthState.Error);
            onAuthError(errorMessage);
        }
    };
    const renderContent = () => {
        switch (authState) {
            case AuthState.Checking:
                return (_jsx(Box, { flexDirection: "column", alignItems: "center", justifyContent: "center", height: 10, children: _jsx(Text, { color: theme.text.primary, children: "\uD83D\uDD0D Checking authentication status..." }) }));
            case AuthState.NewUser:
                return _jsx(SixthWelcomeScreen, { onContinue: handleContinueAuth });
            case AuthState.Authenticating:
                return (_jsxs(Box, { flexDirection: "column", alignItems: "center", justifyContent: "center", height: 15, children: [_jsx(Box, { marginBottom: 2, children: _jsx(Text, { color: theme.text.accent, bold: true, children: "SIXTH" }) }), _jsx(Text, { color: theme.text.primary, children: "\uD83C\uDF10 Authenticating with Sixth..." }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: "Please complete the authentication in your browser" }) }), _jsx(Box, { marginTop: 2, children: _jsx(Text, { color: theme.text.secondary, italic: true, children: "Waiting for authentication to complete..." }) })] }));
            case AuthState.Error:
                return (_jsxs(Box, { flexDirection: "column", alignItems: "center", justifyContent: "center", height: 15, children: [_jsx(Box, { marginBottom: 2, children: _jsx(Text, { color: theme.text.accent, bold: true, children: "SIXTH" }) }), _jsx(Text, { color: theme.status.error, children: "\u274C Authentication Error" }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: error }) }), _jsx(Box, { marginTop: 2, children: _jsx(Text, { color: theme.text.secondary, children: "Please restart the CLI and try again" }) })] }));
            case AuthState.Success:
                return (_jsxs(Box, { flexDirection: "column", alignItems: "center", justifyContent: "center", height: 10, children: [_jsx(Text, { color: theme.status.success, children: "\u2705 Authentication successful!" }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: theme.text.secondary, children: "Starting Sixth CLI..." }) })] }));
            default:
                return null;
        }
    };
    return (_jsx(Box, { flexDirection: "column", width: "100%", children: renderContent() }));
};
//# sourceMappingURL=SixthAuthHandler.js.map