import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState } from 'react';
import { Box, Text } from 'ink';
import { useKeypress } from '../../hooks/useKeypress.js';
import { Command } from '../../key/keyMatchers.js';
import { useKeyMatchers } from '../../hooks/useKeyMatchers.js';
import { theme } from '../../semantic-colors.js';
export function ExtensionDetails({ extension, onBack, onInstall, isInstalled, }) {
    const keyMatchers = useKeyMatchers();
    const [consentRequest, setConsentRequest] = useState(null);
    const [isInstalling, setIsInstalling] = useState(false);
    useKeypress((key) => {
        if (consentRequest) {
            if (keyMatchers[Command.ESCAPE](key)) {
                consentRequest.resolve(false);
                setConsentRequest(null);
                setIsInstalling(false);
                return true;
            }
            if (keyMatchers[Command.RETURN](key)) {
                consentRequest.resolve(true);
                setConsentRequest(null);
                return true;
            }
            return false;
        }
        if (keyMatchers[Command.ESCAPE](key)) {
            onBack();
            return true;
        }
        if (keyMatchers[Command.RETURN](key) && !isInstalled && !isInstalling) {
            setIsInstalling(true);
            void onInstall((prompt) => new Promise((resolve) => {
                setConsentRequest({ prompt, resolve });
            }));
            return true;
        }
        return false;
    }, { isActive: true, priority: true });
    if (consentRequest) {
        return (_jsxs(Box, { flexDirection: "column", paddingX: 1, paddingY: 0, height: "100%", borderStyle: "round", borderColor: theme.status.warning, children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: theme.text.primary, children: consentRequest.prompt }) }), _jsx(Box, { flexGrow: 1 }), _jsxs(Box, { flexDirection: "row", justifyContent: "space-between", marginTop: 1, children: [_jsx(Text, { color: theme.text.secondary, children: "[Esc] Cancel" }), _jsx(Text, { color: theme.text.primary, children: "[Enter] Accept" })] })] }));
    }
    if (isInstalling) {
        return (_jsx(Box, { flexDirection: "column", paddingX: 1, paddingY: 0, height: "100%", borderStyle: "round", borderColor: theme.border.default, justifyContent: "center", alignItems: "center", children: _jsxs(Text, { color: theme.text.primary, children: ["Installing ", extension.extensionName, "..."] }) }));
    }
    return (_jsxs(Box, { flexDirection: "column", paddingX: 1, paddingY: 0, height: "100%", borderStyle: "round", borderColor: theme.border.default, children: [_jsxs(Box, { flexDirection: "row", justifyContent: "space-between", marginBottom: 1, children: [_jsxs(Box, { children: [_jsxs(Text, { color: theme.text.secondary, children: ['>', " Extensions ", '>', ' '] }), _jsx(Text, { color: theme.text.primary, bold: true, children: extension.extensionName })] }), _jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: theme.text.secondary, children: [extension.extensionVersion ? `v${extension.extensionVersion}` : '', ' ', "|", ' '] }), _jsx(Text, { color: theme.status.warning, children: "\u2B50 " }), _jsxs(Text, { color: theme.text.secondary, children: [String(extension.stars || 0), " |", ' '] }), extension.isGoogleOwned && (_jsx(Text, { color: theme.text.primary, children: "[G] " })), _jsx(Text, { color: theme.text.primary, children: extension.fullName })] })] }), _jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: theme.text.primary, children: extension.extensionDescription || extension.repoDescription }) }), _jsx(Box, { flexDirection: "row", marginBottom: 1, children: [
                    extension.hasMCP && { label: 'MCP', color: theme.text.primary },
                    extension.hasContext && {
                        label: 'Context file',
                        color: theme.status.error,
                    },
                    extension.hasHooks && { label: 'Hooks', color: theme.status.warning },
                    extension.hasSkills && {
                        label: 'Skills',
                        color: theme.status.success,
                    },
                    extension.hasCustomCommands && {
                        label: 'Commands',
                        color: theme.text.primary,
                    },
                ]
                    .filter((f) => !!f)
                    .map((feature, index, array) => (_jsxs(Box, { flexDirection: "row", children: [_jsxs(Text, { color: feature.color, children: [feature.label, " "] }), index < array.length - 1 && (_jsx(Box, { marginRight: 1, children: _jsx(Text, { color: theme.text.secondary, children: "|" }) }))] }, feature.label))) }), extension.hasMCP && (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { color: theme.text.primary, children: "This extension will run the following MCP servers:" }), _jsx(Box, { marginLeft: 2, children: _jsxs(Text, { color: theme.text.primary, children: ["* ", extension.extensionName, " (local)"] }) })] })), extension.hasContext && (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsx(Text, { color: theme.text.primary, children: "This extension will append info to your gemini.md context using gemini.md" }) })), _jsx(Box, { flexGrow: 1 }), !isInstalled && (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: theme.status.warning, paddingX: 1, paddingY: 0, children: [_jsxs(Text, { color: theme.text.primary, children: ["The extension you are about to install may have been created by a third-party developer and sourced", '\n', "from a public repository. Google does not vet, endorse, or guarantee the functionality or security", '\n', "of extensions. Please carefully inspect any extension and its source code before installing to", '\n', "understand the permissions it requires and the actions it may perform."] }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: theme.text.primary, children: ["[", 'Enter', "] Install"] }) })] })), isInstalled && (_jsx(Box, { flexDirection: "row", marginTop: 1, justifyContent: "center", children: _jsx(Text, { color: theme.status.success, children: "Already Installed" }) }))] }));
}
//# sourceMappingURL=ExtensionDetails.js.map