import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * /tools Command
 *
 * Lists every tool currently registered, grouped by source: built-in, MCP,
 * and custom (file-based). Lets users verify which tools the model has
 * access to in the current session.
 */
import { Box, Text } from 'ink';
import React from 'react';
import { TitledBoxWithPreferences } from '../components/ui/titled-box.js';
import { useTerminalWidth } from '../hooks/useTerminalWidth.js';
import { useTheme } from '../hooks/useTheme.js';
import { getToolManager } from '../message-handler.js';
import { generateKey } from '../session/key-generator.js';
import { findSkill } from '../skills/skill-registry.js';
import { infoMsg } from '../utils/message-factory.js';
export function groupTools(toolManager) {
    const all = toolManager.getToolNames().sort();
    const builtin = [];
    const mcp = [];
    const custom = [];
    for (const name of all) {
        if (toolManager.isCustomTool(name)) {
            const info = toolManager.getCustomToolInfo(name);
            custom.push({ name, source: info?.source ?? 'project' });
            continue;
        }
        // Bundle-form skill tools: tagged with `ownerSkill` via the registrar.
        // When their bundle sets `tools_visibility.default: global` they
        // belong in /tools's Custom group alongside flat-form tools.
        const ownerSkill = toolManager.getOwnerSkill(name);
        if (ownerSkill) {
            const skill = findSkill(ownerSkill);
            custom.push({
                name,
                source: skill?.source.priority ?? 'project',
                ownerSkill,
            });
            continue;
        }
        const mcpInfo = toolManager.getMCPToolInfo(name);
        if (mcpInfo.isMCPTool) {
            mcp.push({ name, server: mcpInfo.serverName });
            continue;
        }
        builtin.push(name);
    }
    return { builtin, mcp, custom };
}
function ToolsView({ toolManager }) {
    const boxWidth = useTerminalWidth();
    const { colors } = useTheme();
    if (!toolManager) {
        return (_jsx(TitledBoxWithPreferences, { title: "Tools", width: boxWidth, borderColor: colors.primary, paddingX: 2, paddingY: 1, flexDirection: "column", marginBottom: 1, children: _jsx(Text, { color: colors.text, children: "Tool manager not yet initialized." }) }));
    }
    const { builtin, mcp, custom } = groupTools(toolManager);
    const total = builtin.length + mcp.length + custom.length;
    return (_jsxs(TitledBoxWithPreferences, { title: `Tools (${total})`, width: boxWidth, borderColor: colors.primary, paddingX: 2, paddingY: 1, flexDirection: "column", marginBottom: 1, children: [_jsx(Box, { marginBottom: 1, children: _jsxs(Text, { color: colors.primary, bold: true, children: ["Built-in (", builtin.length, ")"] }) }), builtin.length === 0 ? (_jsx(Text, { color: colors.secondary, children: "(none)" })) : (_jsx(Text, { color: colors.text, children: builtin.join(', ') })), _jsx(Box, { marginTop: 1, marginBottom: 1, children: _jsxs(Text, { color: colors.primary, bold: true, children: ["MCP (", mcp.length, ")"] }) }), mcp.length === 0 ? (_jsx(Text, { color: colors.secondary, children: "(none)" })) : (mcp.map(t => (_jsxs(Text, { color: colors.text, children: ["\u203A ", t.name, t.server ? (_jsxs(Text, { color: colors.secondary, children: [" (", t.server, ")"] })) : null] }, t.name)))), _jsx(Box, { marginTop: 1, marginBottom: 1, children: _jsxs(Text, { color: colors.primary, bold: true, children: ["Custom (", custom.length, ")"] }) }), custom.length === 0 ? (_jsx(Text, { color: colors.secondary, children: "(none)" })) : (custom.map(t => (_jsxs(Text, { color: colors.text, children: ["\u203A ", t.name, ' ', _jsxs(Text, { color: colors.secondary, children: ["(", t.ownerSkill ? `skill:${t.ownerSkill}, ` : '', t.source, ")"] })] }, t.name))))] }));
}
export const toolsCommand = {
    name: 'tools',
    description: 'List available tools (built-in, MCP, custom). Subcommand: create <name>',
    handler: (args, _messages, _metadata) => {
        // `create` is intercepted earlier in app-util.ts and dispatched to
        // handleToolCreate, but if a user reaches here with `create` and no
        // name (e.g. from a non-app context), show usage rather than a list.
        if (args[0] === 'create') {
            return Promise.resolve(infoMsg('Usage: /tools create <name>\nExample: /tools create k8s-pods\n\nThis creates a new custom tool file under .nanocoder/tools/ and starts an AI-assisted session to write its content.', 'tools'));
        }
        const toolManager = getToolManager();
        return Promise.resolve(React.createElement(ToolsView, {
            key: generateKey('tools'),
            toolManager,
        }));
    },
};
//# sourceMappingURL=tools.js.map