import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text, useInput } from "ink";
import SelectInput from "ink-select-input";
import { useState } from "react";
export function AgentSelector({ agents, defaultAgentName, onComplete }) {
    const [step, setStep] = useState("select-agent");
    const [customCommand, setCustomCommand] = useState("");
    // Build items for agent selection, with default at top if it exists
    const buildAgentItems = () => {
        const items = [];
        // Sort agents: available first (with default at very top), then unavailable
        const sortedAgents = [...agents].sort((a, b) => {
            // First, separate available from unavailable
            if (a.available && !b.available)
                return -1;
            if (!a.available && b.available)
                return 1;
            // Within available agents, put default first
            if (a.available && b.available) {
                if (a.name === defaultAgentName)
                    return -1;
                if (b.name === defaultAgentName)
                    return 1;
            }
            return 0;
        });
        for (const agent of sortedAgents) {
            items.push({
                label: agent.available ? agent.displayName : `${agent.displayName} (not installed)`,
                value: agent.available ? agent.name : `disabled:${agent.name}`
            });
        }
        items.push({ label: "Custom command...", value: "custom" });
        items.push({ label: "d3k (no agent)", value: "none" });
        items.push({ label: "d3k --debug", value: "debug" });
        return items;
    };
    const agentItems = buildAgentItems();
    // Handle agent selection
    const handleAgentSelect = (item) => {
        if (item.value.startsWith("disabled:")) {
            return; // Ignore disabled items
        }
        if (item.value === "none") {
            // User chose no agent
            onComplete({ agent: null });
            return;
        }
        if (item.value === "debug") {
            // User chose debug mode
            onComplete({ agent: { name: "debug", command: "debug" } });
            return;
        }
        if (item.value === "custom") {
            setStep("custom-input");
            return;
        }
        // Simple agent selection - complete immediately
        const agent = agents.find((a) => a.name === item.value);
        // Use custom command if specified, otherwise fall back to binary name
        onComplete({ agent: { name: item.value, command: agent?.command || agent?.binary || item.value } });
    };
    // Handle custom command input
    useInput((input, key) => {
        if (step !== "custom-input")
            return;
        if (key.return) {
            if (customCommand.trim()) {
                onComplete({ agent: { name: "custom", command: customCommand.trim() } });
            }
            return;
        }
        if (key.backspace || key.delete) {
            setCustomCommand((prev) => prev.slice(0, -1));
            return;
        }
        if (key.escape) {
            setStep("select-agent");
            setCustomCommand("");
            return;
        }
        // Regular character input
        if (input && !key.ctrl && !key.meta) {
            setCustomCommand((prev) => prev + input);
        }
    }, { isActive: step === "custom-input" });
    // Render based on current step
    if (step === "select-agent") {
        return (_jsxs(Box, { flexDirection: "column", paddingX: 1, children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: "#A18CE5", bold: true, children: "d3k Agent Selection" }) }), _jsx(Text, { dimColor: true, children: "Select an AI agent to run alongside d3k:" }), _jsx(Box, { marginTop: 1, children: _jsx(SelectInput, { items: agentItems, onSelect: handleAgentSelect, indicatorComponent: ({ isSelected }) => _jsx(Text, { color: "#A18CE5", children: isSelected ? "❯ " : "  " }), itemComponent: ({ isSelected, label }) => {
                            const isDisabled = label.includes("not installed");
                            return (_jsx(Text, { color: isDisabled ? "gray" : isSelected ? "#A18CE5" : undefined, dimColor: isDisabled, children: label }));
                        } }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Use \u2191\u2193 to navigate, Enter to select" }) })] }));
    }
    if (step === "custom-input") {
        return (_jsxs(Box, { flexDirection: "column", paddingX: 1, children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: "#A18CE5", bold: true, children: "Custom Command" }) }), _jsx(Text, { dimColor: true, children: "Enter the command to run your agent:" }), _jsxs(Box, { marginTop: 1, children: [_jsx(Text, { color: "#A18CE5", children: "\u276F " }), _jsx(Text, { children: customCommand }), _jsx(Text, { color: "gray", children: "\u258C" })] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Press Enter to confirm, Esc to go back" }) })] }));
    }
    return null;
}
//# sourceMappingURL=AgentSelector.js.map