/**
 * TUI renderers for meta/orchestration tools.
 *
 * Covers get_tool_info, execute_tool, mcp_describe, mcp (execute),
 * llm_analysis, switch_mode, and a generic text result renderer.
 */
import { basename } from 'node:path';
import { Text } from '../../../../tui/index.js';
/** Render get_tool_info call — shows which tool is being retrieved. */
export function renderGetToolInfoCall(args, theme) {
    const parsedArgs = args && typeof args === 'object' ? args : undefined;
    const toolName = typeof parsedArgs?.tool_name === 'string' ? parsedArgs.tool_name : '…';
    return new Text(theme.fg('dim', `Retrieving "${toolName}" tool`), 0, 0);
}
/** Hide result when collapsed, show nothing when expanded (lets parent handle display). */
export function renderHiddenExpandedResult(expanded) {
    if (!expanded)
        return new Text('', 0, 0);
    return undefined;
}
/** Render execute_tool call — tool name header with parameter details when expanded. */
export function renderExecuteToolCall(args, expanded, theme) {
    const parsedArgs = args && typeof args === 'object'
        ? args
        : undefined;
    const toolName = typeof parsedArgs?.tool_name === 'string' ? parsedArgs.tool_name : '…';
    const params = parsedArgs?.parameters && typeof parsedArgs.parameters === 'object'
        ? parsedArgs.parameters
        : {};
    const title = theme.fg('toolTitle', theme.bold(`Executing "${toolName}" tool`));
    if (!expanded) {
        return new Text(title, 0, 0);
    }
    const paramLines = Object.entries(params)
        .filter(([, value]) => value !== undefined)
        .map(([key, value]) => `  ${key}: ${typeof value === 'string' ? value : JSON.stringify(value)}`);
    const body = paramLines.length > 0 ? '\n' + paramLines.join('\n') : '';
    return new Text(`${title}${body}`, 0, 0);
}
/** Render execute_tool result — collapsed shows first line preview, expanded defers to parent. */
export function renderExecuteToolResult(result, expanded, theme) {
    if (expanded)
        return undefined;
    const textBlocks = result?.content?.filter((block) => block.type === 'text') ?? [];
    const raw = textBlocks.map((block) => block.text ?? '').join('\n');
    const firstLine = raw.split('\n')[0]?.trim();
    if (firstLine) {
        return new Text(theme.fg('dim', `  ${firstLine}`), 0, 0);
    }
    return new Text('', 0, 0);
}
/** Render mcp_describe call — shows server/tool being inspected. */
export function renderMcpDescribeCall(args, theme) {
    const parsedArgs = args && typeof args === 'object' ? args : undefined;
    const server = typeof parsedArgs?.server === 'string' ? parsedArgs.server : '';
    const tool = typeof parsedArgs?.tool === 'string' ? parsedArgs.tool : '';
    const label = tool ? `${server}/${tool}` : server;
    return new Text(theme.fg('toolTitle', theme.bold(`🔎 ${label}`)), 0, 0);
}
/** Render MCP tool execution call — server/tool header with arguments when expanded. */
export function renderMcpExecuteCall(args, expanded, theme) {
    const parsedArgs = args && typeof args === 'object'
        ? args
        : undefined;
    const server = typeof parsedArgs?.server === 'string' ? parsedArgs.server : '';
    const tool = typeof parsedArgs?.tool === 'string' ? parsedArgs.tool : '';
    const title = theme.fg('toolTitle', theme.bold(`⚡ ${server}/${tool}`));
    if (!expanded) {
        return new Text(title, 0, 0);
    }
    const argsObj = parsedArgs?.arguments && typeof parsedArgs.arguments === 'object'
        ? parsedArgs.arguments
        : undefined;
    const details = argsObj && Object.keys(argsObj).length > 0
        ? Object.entries(argsObj)
            .map(([key, value]) => `  ${key}: ${typeof value === 'string' ? value : JSON.stringify(value)}`)
            .join('\n')
        : '';
    const body = details ? `\n${theme.fg('toolOutput', details)}` : '';
    return new Text(`${title}${body}`, 0, 0);
}
/** Render llm_analysis call — description header with file list and model when expanded. */
export function renderLlmAnalysisCall(args, expanded, theme, defaultModel) {
    const parsedArgs = args && typeof args === 'object'
        ? args
        : undefined;
    const desc = typeof parsedArgs?.description === 'string' ? parsedArgs.description : '';
    const label = desc || 'llm_analysis';
    const title = theme.fg('toolTitle', theme.bold(`🔍 ${label}`));
    if (!expanded) {
        return new Text(title, 0, 0);
    }
    const files = Array.isArray(parsedArgs?.file_paths)
        ? parsedArgs.file_paths.map((fp) => `  ${basename(String(fp))}`).join('\n')
        : '';
    const model = typeof parsedArgs?.model === 'string' ? parsedArgs.model : defaultModel;
    const details = [
        files ? `Files:\n${files}` : '',
        `Model: ${model}`,
        typeof parsedArgs?.query === 'string' ? `Query: ${parsedArgs.query}` : ''
    ]
        .filter(Boolean)
        .join('\n');
    return new Text(`${title}\n\n${theme.fg('toolOutput', details)}`, 0, 0);
}
/** Collapse whitespace and truncate `text` to a single-line preview. */
function buildSingleLinePreview(text, maxLength = 140) {
    const normalized = text.replace(/\s+/g, ' ').trim();
    if (normalized.length <= maxLength)
        return normalized;
    return `${normalized.slice(0, maxLength - 1).trimEnd()}…`;
}
/** Render switch_mode call — show target mode plus handoff reason/query. */
export function renderSwitchModeCall(args, expanded, theme) {
    const parsedArgs = args && typeof args === 'object' ? args : undefined;
    const mode = typeof parsedArgs?.mode === 'string' ? parsedArgs.mode : '…';
    const title = theme.fg('toolTitle', `Switching to ${mode} mode...`);
    const reason = typeof parsedArgs?.reason === 'string' ? parsedArgs.reason.trim() : '';
    if (!reason) {
        return new Text(title, 0, 0);
    }
    const displayReason = expanded ? reason : buildSingleLinePreview(reason);
    return new Text(`${title}\n${theme.fg('dim', `  ${displayReason}`)}`, 0, 0);
}
/** Render switch_mode result — first line only, even when the raw result includes the reason. */
export function renderSwitchModeResult(result, theme) {
    const textBlocks = result?.content?.filter((block) => block.type === 'text') ?? [];
    const output = textBlocks
        .map((block) => block.text ?? '')
        .join('\n')
        .trim();
    if (!output)
        return undefined;
    const firstLine = output.split('\n')[0]?.trim();
    return firstLine ? new Text(theme.fg('toolOutput', firstLine), 0, 0) : undefined;
}
/** Render send_message without exposing target ids or raw JSON arguments. */
export function renderSendMessageCall(args, theme) {
    const parsedArgs = args && typeof args === 'object' ? args : undefined;
    const message = typeof parsedArgs?.message === 'string' ? parsedArgs.message : '';
    const preview = message ? `: ${buildSingleLinePreview(message, 120)}` : '';
    return new Text(theme.fg('toolTitle', `${theme.bold('Resuming subagent')}${preview}`), 0, 0);
}
/** Render send_message result as a compact single-line acknowledgement. */
export function renderSendMessageResult(result, theme) {
    const isError = result?.isError === true;
    return new Text(theme.fg(isError ? 'error' : 'success', isError ? 'message failed' : 'message queued'), 0, 0);
}
/** Generic text result renderer — shows full text output only when expanded. */
export function renderTextResult(result, expanded, theme) {
    if (!expanded)
        return undefined;
    const textBlocks = result?.content?.filter((block) => block.type === 'text') ?? [];
    const output = textBlocks.map((block) => block.text ?? '').join('\n');
    if (!output)
        return undefined;
    return new Text(theme.fg('toolOutput', output), 0, 0);
}
//# sourceMappingURL=tool-meta.js.map