/**
 * TUI renderers for the bash tool.
 *
 * renderBashCall: shows the command (or description) as a styled header.
 * renderBashResult: shows output with truncation/exit-code warnings.
 */
import { Text } from '../../../../tui/index.js';
import { formatSize } from '../../../../utils/truncate.js';
import { editorKey } from '../../../components/primitives/keybinding-hints.js';
import { coerceToolString } from '../../runtime/tool-execution-types.js';
function invalidArg(theme) {
    return theme.fg('error', '[invalid arg]');
}
/** Render the bash tool call header — shows the command or description. */
export function renderBashCall(args, expanded, theme) {
    const parsedArgs = (args ?? {});
    const command = coerceToolString(parsedArgs.command);
    const description = coerceToolString(parsedArgs.description);
    const timeout = typeof parsedArgs.timeout === 'number' ? parsedArgs.timeout : undefined;
    const timeoutSuffix = timeout ? theme.fg('muted', ` (timeout ${timeout}s)`) : '';
    const headerText = expanded
        ? command === null
            ? invalidArg(theme)
            : command
                ? command
                : theme.fg('toolOutput', '...')
        : description || command || '...';
    return new Text(theme.fg('toolTitle', theme.bold(`${headerText}`)) + timeoutSuffix, 0, 0);
}
/**
 * Render the bash tool result — output lines, truncation info, and status warnings.
 * Collapsed view shows a 2-line preview; expanded shows the full output.
 */
export function renderBashResult(result, expanded, textOutput, terminalColumns, theme) {
    const trimmedOutput = textOutput.trim();
    const lines = [];
    if (trimmedOutput) {
        const outputLines = trimmedOutput.split('\n');
        const outputColor = result?.isError ? 'errorDim' : 'toolOutput';
        if (expanded) {
            lines.push(...outputLines.map((line, index) => {
                const prefix = index === 0 ? `${theme.fg('muted', '⎿')}  ` : '   ';
                return prefix + theme.fg(outputColor, line);
            }));
        }
        else {
            const maxWidth = terminalColumns - 9;
            const previewLines = outputLines
                .slice(-2)
                .map((line) => (line.length > maxWidth ? line.slice(0, maxWidth - 1) + '…' : line));
            const remaining = outputLines.length - 2;
            lines.push(...previewLines.map((line, index) => {
                const prefix = index === 0 ? `${theme.fg('muted', '⎿')}  ` : '   ';
                return prefix + theme.fg(outputColor, line);
            }));
            if (remaining > 0) {
                lines.push(`   ${theme.fg('muted', `… +${remaining} lines (${editorKey('expandTools')} to expand)`)}`);
            }
        }
    }
    const truncation = result?.details?.truncation;
    const fullOutputPath = result?.details?.fullOutputPath;
    const exitCode = result?.details?.exitCode;
    const cancelled = result?.details?.cancelled;
    const timedOut = result?.details?.timedOut;
    if (truncation?.truncated || fullOutputPath || cancelled || timedOut || exitCode !== undefined) {
        const warnings = [];
        if (cancelled)
            warnings.push('Cancelled');
        if (timedOut)
            warnings.push('Timed out');
        if (exitCode !== undefined)
            warnings.push(`Exit code: ${exitCode}`);
        if (fullOutputPath) {
            warnings.push(`Full output: ${fullOutputPath}`);
        }
        if (truncation?.truncated &&
            typeof truncation.outputChars === 'number' &&
            typeof truncation.totalChars === 'number') {
            warnings.push(`Truncated: showing ${formatSize(truncation.outputChars)} of ${formatSize(truncation.totalChars)}`);
        }
        lines.push(theme.fg('warning', `[${warnings.join('. ')}]`));
    }
    if (lines.length === 0) {
        return undefined;
    }
    return new Text(lines.join('\n'), 0, 0);
}
//# sourceMappingURL=bash.js.map