import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/**
 * Agents Command
 *
 * Lists all available subagents with their configurations.
 * Supports `/agents show <name>` to view a full agent definition.
 */
import { Box, Text } from 'ink';
import React from 'react';
import { InfoField } from '../components/ui/info-field.js';
import { TitledBoxWithPreferences } from '../components/ui/titled-box.js';
import { useResponsiveTerminal } from '../hooks/useTerminalWidth.js';
import { useTheme } from '../hooks/useTheme.js';
import { parseMarkdown } from '../markdown-parser/index.js';
import { generateKey } from '../session/key-generator.js';
import { getSubagentLoader } from '../subagents/subagent-loader.js';
import { errorMsg } from '../utils/message-factory.js';
import { wrapWithTrimmedContinuations } from '../utils/text-wrapping.js';
function SubagentsList({ subagents }) {
    const { colors } = useTheme();
    const { boxWidth, truncate } = useResponsiveTerminal();
    const descMaxLen = Math.max(boxWidth - 12, 20);
    return (_jsx(TitledBoxWithPreferences, { title: "Subagents", width: boxWidth, borderColor: colors.primary, paddingX: 2, paddingY: 1, flexDirection: "column", marginBottom: 1, children: subagents.length === 0 ? (_jsxs(_Fragment, { children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: colors.text, bold: true, children: "No subagents found" }) }), _jsxs(Text, { color: colors.secondary, children: ["Create agents in", ' ', _jsx(Text, { color: colors.primary, children: ".nanocoder/agents/" }), " or use", ' ', _jsxs(Text, { color: colors.primary, children: ["/agents create ", '<name>'] })] })] })) : (subagents.map((agent, i) => (_jsxs(Box, { flexDirection: "column", marginBottom: i < subagents.length - 1 ? 1 : 0, children: [_jsxs(Box, { children: [_jsxs(Text, { color: colors.text, bold: true, children: ["\u203A ", agent.name] }), _jsxs(Text, { color: colors.secondary, children: [' ', "\u00B7 ", agent.model || 'inherit'] })] }), _jsx(Box, { marginLeft: 4, children: _jsx(Text, { color: colors.secondary, children: truncate(agent.description, descMaxLen) }) }), _jsx(Box, { marginLeft: 4, children: _jsx(Text, { color: colors.secondary, children: agent.tools
                            ? `${agent.tools.length} tool${agent.tools.length === 1 ? '' : 's'}`
                            : 'all tools' }) })] }, agent.name)))) }));
}
function AgentDetail({ agent }) {
    const { colors } = useTheme();
    const { boxWidth } = useResponsiveTerminal();
    const textWidth = boxWidth - 6; // account for border + padding
    const sourceLabel = agent.source.isBuiltIn
        ? 'built-in'
        : agent.source.filePath || 'unknown';
    const toolsSummary = agent.tools
        ? agent.tools.join(', ')
        : 'all (filtered by config)';
    const fields = [
        { label: 'Source', value: sourceLabel },
        ...(agent.provider ? [{ label: 'Provider', value: agent.provider }] : []),
        { label: 'Model', value: agent.model || 'inherit' },
        { label: 'Tools', value: toolsSummary },
        ...(agent.disallowedTools && agent.disallowedTools.length > 0
            ? [{ label: 'Disallowed tools', value: agent.disallowedTools.join(', ') }]
            : []),
    ];
    const descriptionRendered = wrapWithTrimmedContinuations(parseMarkdown(agent.description, colors, textWidth), textWidth);
    const systemPromptRendered = wrapWithTrimmedContinuations(parseMarkdown(agent.systemPrompt, colors, textWidth), textWidth);
    return (_jsxs(TitledBoxWithPreferences, { title: `Agent: ${agent.name}`, width: boxWidth, borderColor: colors.primary, paddingX: 2, paddingY: 1, flexDirection: "column", marginBottom: 1, children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { children: descriptionRendered }) }), fields.map(f => (_jsx(InfoField, { label: f.label, value: f.value }, f.label))), _jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { color: colors.primary, bold: true, children: "System prompt" }), _jsx(Text, { children: systemPromptRendered })] })] }));
}
export const agentsCommand = {
    name: 'agents',
    description: 'List subagents. /agents show <name> for details, /agents copy <name> to customize',
    handler: async (args) => {
        const loader = getSubagentLoader();
        // /agents show <name>
        if (args[0] === 'show' && args[1]) {
            const agent = await loader.getSubagent(args[1]);
            if (!agent) {
                return errorMsg(`Agent '${args[1]}' not found. Run /agents to see available agents.`, 'agents-show-error');
            }
            return React.createElement(AgentDetail, {
                key: generateKey('agents-show'),
                agent,
            });
        }
        // /agents — list all
        const agents = await loader.listSubagents();
        const formatted = agents.map(agent => ({
            name: agent.name,
            description: agent.description,
            model: agent.model,
            tools: agent.tools,
            isBuiltIn: agent.source.isBuiltIn,
        }));
        return React.createElement(SubagentsList, {
            key: generateKey('agents'),
            subagents: formatted,
        });
    },
};
//# sourceMappingURL=agents.js.map