import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text, useStdout } from "ink";
import { Pane } from "./design/Pane.js";
import { StatusBadge } from "./design/StatusBadge.js";
const CATEGORIES = [
    {
        title: "Start Here",
        description: "Setup, diagnostics, and current runtime state.",
        names: ["doctor", "init", "status", "model", "config", "version", "help"],
    },
    {
        title: "Conversation",
        description: "Sessions, transcripts, tags, and context compaction.",
        names: ["resume", "rename", "sessions", "tag", "compact", "clear", "export"],
    },
    {
        title: "Context And Cache",
        description: "Project files, extra workspaces, memory, DeepSeek cache planning.",
        names: ["files", "context", "add-dir", "memory", "cache", "effort", "usage", "stats", "cost"],
    },
    {
        title: "Workflows",
        description: "Plans, todos, task queues, run control, and side questions.",
        names: ["plan", "question", "todos", "todo", "tasks", "runs", "attach", "queue", "pause", "run-resume", "cancel", "btw"],
    },
    {
        title: "Code Review",
        description: "Git state, diffs, review prompts, and validation gates.",
        names: ["branch", "diff", "review", "security-review", "validation", "trace", "events", "logs"],
    },
    {
        title: "Tools And Extensions",
        description: "Agents, skills, plugins, hooks, MCP, browser, SSH, and permissions.",
        names: ["tools", "cmd", "shell", "agents", "skills", "plugins", "hooks", "mcp", "browser", "ssh", "permissions", "approval", "multi"],
    },
];
export function HelpPanel(props) {
    const { stdout } = useStdout();
    const width = Math.max(64, Math.min(stdout.columns ?? 100, 118));
    const commands = visibleUniqueCommands(props.commands);
    const commandByName = new Map(commands.map((command) => [command.name, command]));
    const categorized = new Set();
    const custom = commands.filter((command) => !CATEGORIES.some((category) => category.names.includes(command.name)));
    return (_jsxs(Pane, { width: width, title: "DeepSeekCode help", tone: "brand", paddingX: 1, children: [_jsxs(Box, { justifyContent: "space-between", paddingTop: 1, children: [_jsxs(Box, { children: [_jsx(StatusBadge, { label: `${commands.length} commands`, tone: "brand" }), _jsx(Text, { color: "gray", children: "  Type /command or use /help to browse." })] }), _jsx(Text, { color: "gray", children: "Esc/Ctrl+C exits the TUI" })] }), _jsxs(Box, { flexDirection: "column", marginTop: 1, children: [CATEGORIES.map((category) => {
                        const sectionCommands = category.names
                            .map((name) => commandByName.get(name))
                            .filter((command) => Boolean(command));
                        for (const command of sectionCommands)
                            categorized.add(command.name);
                        return (_jsx(HelpSection, { title: category.title, description: category.description, commands: sectionCommands, width: width }, category.title));
                    }), custom.length > 0 && (_jsx(HelpSection, { title: "Project Commands", description: "Commands discovered from project, user, cache, or plugins.", commands: custom.filter((command) => !categorized.has(command.name)), width: width }))] })] }));
}
function HelpSection(props) {
    if (props.commands.length === 0)
        return null;
    const commandWidth = props.width >= 96 ? 29 : 24;
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Box, { children: [_jsx(Text, { color: "cyan", bold: true, children: props.title }), _jsx(Text, { color: "gray", children: `  ${props.description}` })] }), _jsx(Box, { flexDirection: "column", paddingLeft: 1, children: props.commands.map((command) => (_jsxs(Box, { children: [_jsx(Text, { color: "cyan", children: padRight(formatUsage(command), commandWidth) }), _jsx(Text, { color: "gray", children: truncate(command.description, Math.max(24, props.width - commandWidth - 6)) })] }, command.name))) })] }));
}
function visibleUniqueCommands(commands) {
    const seen = new Set();
    const visible = [];
    for (const command of commands) {
        if (command.hidden || seen.has(command.name))
            continue;
        seen.add(command.name);
        visible.push(command);
    }
    return visible.sort((a, b) => a.name.localeCompare(b.name));
}
function formatUsage(command) {
    return `/${command.name}${command.usage ? ` ${command.usage}` : ""}`;
}
function padRight(value, width) {
    if (value.length >= width)
        return `${value.slice(0, width - 3)}...`;
    return `${value}${" ".repeat(width - value.length)}`;
}
function truncate(value, width) {
    if (value.length <= width)
        return value;
    return `${value.slice(0, Math.max(0, width - 3))}...`;
}
