import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { Terminal } from 'lucide-react';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { prism, vscDarkPlus, } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { formatDuration } from './utils';
import { ToolErrorDisplay } from './ToolErrorDisplay';
import { ToolHeader, ToolHeaderSeparator, ToolHeaderMeta, ToolHeaderSuccess, ToolHeaderError, ToolContentBox, } from './shared';
export function BashRenderer({ contentJson, toolDurationMs, isExpanded, onToggle, compact, }) {
    const result = contentJson.result || {};
    const args = contentJson.args || {};
    const hasToolError = typeof result === 'object' && 'ok' in result && result.ok === false;
    const errorMessage = hasToolError && 'error' in result && typeof result.error === 'string'
        ? result.error
        : null;
    const errorStack = hasToolError && 'stack' in result && typeof result.stack === 'string'
        ? result.stack
        : undefined;
    const stdout = String(result.stdout || '');
    const stderr = String(result.stderr || '');
    const exitCode = Number(result.exitCode ?? 0);
    const cmd = String(args.cmd || '');
    const cwd = args.cwd ? String(args.cwd) : undefined;
    const timeStr = formatDuration(toolDurationMs);
    const syntaxTheme = document?.documentElement.classList.contains('dark')
        ? vscDarkPlus
        : prism;
    const hasOutput = stdout.length > 0 || stderr.length > 0;
    const isError = hasToolError || exitCode !== 0;
    const hasStderr = stderr.length > 0;
    const combinedOutput = stdout + (stdout && stderr ? '\n' : '') + stderr;
    return (_jsxs("div", { className: "text-xs", children: [_jsxs(ToolHeader, { toolName: "bash", isExpanded: isExpanded, onToggle: onToggle, isError: isError, colorVariant: "default", canExpand: true, children: [!compact && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), _jsx("span", { className: "text-foreground/70 min-w-0 truncate font-mono text-[11px]", title: cmd, children: cmd })] })), !hasToolError && !compact && (_jsxs(_Fragment, { children: [_jsx(ToolHeaderSeparator, {}), exitCode === 0 ? (_jsxs(ToolHeaderSuccess, { children: ["exit ", exitCode] })) : (_jsxs(ToolHeaderError, { children: ["exit ", exitCode] })), _jsx(ToolHeaderSeparator, {}), _jsx(ToolHeaderMeta, { children: timeStr })] })), hasToolError && !compact && (_jsxs(ToolHeaderMeta, { children: ["\u00B7 ", timeStr] }))] }), isExpanded && hasToolError && errorMessage && (_jsx(ToolErrorDisplay, { error: errorMessage, stack: errorStack, showStack: true })), isExpanded && !hasToolError && (_jsxs("div", { className: "mt-2 ml-5 flex flex-col gap-2 max-w-full", children: [_jsx(ToolContentBox, { title: "command", icon: _jsx(Terminal, { className: "h-3 w-3" }), subtitle: cwd && cwd !== '.' ? `in ${cwd}` : undefined, copyText: cmd, maxHeight: "", children: _jsx("div", { className: "px-3 py-2 font-mono text-xs bg-muted/10 break-all", children: cmd }) }), hasOutput &&
                        (exitCode === 0 ? (_jsx(ToolContentBox, { title: "output", copyText: combinedOutput, maxHeight: "max-h-[30rem]", children: _jsx(SyntaxHighlighter, { language: "bash", style: syntaxTheme, customStyle: {
                                    margin: 0,
                                    padding: '0.75rem',
                                    fontSize: '0.75rem',
                                    lineHeight: '1.5',
                                    background: 'transparent',
                                    maxWidth: '100%',
                                }, wrapLines: true, wrapLongLines: true, children: combinedOutput }) })) : (_jsxs(_Fragment, { children: [stdout && (_jsx(ToolContentBox, { title: "stdout", copyText: stdout, maxHeight: "max-h-60", children: _jsx(SyntaxHighlighter, { language: "bash", style: syntaxTheme, customStyle: {
                                            margin: 0,
                                            padding: '0.75rem',
                                            fontSize: '0.75rem',
                                            lineHeight: '1.5',
                                            background: 'transparent',
                                            maxWidth: '100%',
                                        }, wrapLines: true, wrapLongLines: true, children: stdout }) })), hasStderr && (_jsx(ToolContentBox, { title: "stderr", copyText: stderr, variant: "error", maxHeight: "max-h-60", children: _jsx(SyntaxHighlighter, { language: "bash", style: syntaxTheme, customStyle: {
                                            margin: 0,
                                            padding: '0.75rem',
                                            fontSize: '0.75rem',
                                            lineHeight: '1.5',
                                            background: 'transparent',
                                            maxWidth: '100%',
                                        }, wrapLines: true, wrapLongLines: true, children: stderr }) }))] })))] }))] }));
}
//# sourceMappingURL=BashRenderer.js.map