import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import { DiffRenderer } from './DiffRenderer.js';
import { Colors } from '../../colors.js';
import { MarkdownDisplay } from '../../utils/MarkdownDisplay.js';
import { AnsiOutputText } from '../AnsiOutput.js';
import { MaxSizedBox } from '../shared/MaxSizedBox.js';
import { useUIState } from '../../contexts/UIStateContext.js';
import { STATUS_INDICATOR_WIDTH } from './ToolShared.js';
const STATIC_HEIGHT = 1;
const RESERVED_LINE_COUNT = 5; // for tool name, status, padding etc.
const MIN_LINES_SHOWN = 2; // show at least this many lines
// Large threshold to ensure we don't cause performance issues for very large
// outputs that will get truncated further MaxSizedBox anyway.
const MAXIMUM_RESULT_DISPLAY_CHARACTERS = 1000000;
export const ToolResultDisplay = ({ resultDisplay, availableTerminalHeight, terminalWidth, renderOutputAsMarkdown = true, }) => {
    const { renderMarkdown } = useUIState();
    const availableHeight = availableTerminalHeight
        ? Math.max(availableTerminalHeight - STATIC_HEIGHT - RESERVED_LINE_COUNT, MIN_LINES_SHOWN + 1)
        : undefined;
    // Long tool call response in MarkdownDisplay doesn't respect availableTerminalHeight properly,
    // we're forcing it to not render as markdown when the response is too long, it will fallback
    // to render as plain text, which is contained within the terminal using MaxSizedBox
    const shouldRenderMarkdown = availableHeight ? false : renderOutputAsMarkdown;
    const childWidth = terminalWidth;
    let displayContent = resultDisplay;
    if (typeof displayContent === 'string') {
        if (displayContent.length > MAXIMUM_RESULT_DISPLAY_CHARACTERS) {
            displayContent =
                '...' + displayContent.slice(-MAXIMUM_RESULT_DISPLAY_CHARACTERS);
        }
    }
    if (!displayContent)
        return null;
    // Check if displayContent is AnsiOutput (array of arrays)
    const isAnsiOutput = Array.isArray(displayContent) &&
        displayContent.length > 0 &&
        Array.isArray(displayContent[0]);
    return (_jsxs(Box, { paddingLeft: STATUS_INDICATOR_WIDTH, width: "100%", marginTop: 1, flexDirection: "column", children: [isAnsiOutput && (_jsx(AnsiOutputText, { data: displayContent, availableTerminalHeight: availableHeight, width: childWidth })), typeof displayContent === 'string' && shouldRenderMarkdown && (_jsx(Box, { flexDirection: "column", children: _jsx(MarkdownDisplay, { text: displayContent, isPending: false, terminalWidth: childWidth, renderMarkdown: renderMarkdown }) })), typeof displayContent === 'string' && !shouldRenderMarkdown && (_jsx(MaxSizedBox, { maxHeight: availableHeight, maxWidth: childWidth, children: _jsx(Box, { children: _jsx(Text, { color: Colors.Foreground, wrap: "wrap", children: displayContent }) }) })), !isAnsiOutput && typeof displayContent !== 'string' && (_jsxs(Box, { flexDirection: "column", children: ['fileDiff' in displayContent && (_jsxs(_Fragment, { children: [(() => {
                                const astValidation = displayContent.metadata?.astValidation;
                                if (!astValidation)
                                    return null;
                                return (_jsx(Box, { marginBottom: 1, children: astValidation.valid ? (_jsx(Text, { color: Colors.AccentGreen, children: "AST Validation Passed" })) : (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: Colors.AccentRed, bold: true, children: "AST Validation Failed" }), astValidation.errors.map((err, i) => (_jsxs(Text, { color: Colors.AccentRed, children: ["- ", err] }, i)))] })) }));
                            })(), _jsx(DiffRenderer, { diffContent: displayContent.fileDiff, filename: displayContent.fileName, availableTerminalHeight: availableHeight, terminalWidth: childWidth })] })), 'content' in displayContent && (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, flexDirection: "column", children: (() => {
                                    const metadata = displayContent.metadata;
                                    const language = metadata?.language;
                                    const declarationsCount = metadata?.declarationsCount;
                                    return (_jsxs(_Fragment, { children: [typeof language === 'string' && (_jsxs(Text, { color: Colors.AccentGreen, children: ["Language: ", language] })), typeof declarationsCount === 'number' && (_jsxs(Text, { color: Colors.AccentGreen, children: ["Declarations Found: ", declarationsCount] }))] }));
                                })() }), _jsx(MarkdownDisplay, { text: displayContent.content, isPending: false, availableTerminalHeight: availableHeight, terminalWidth: childWidth, renderMarkdown: renderMarkdown })] }))] }))] }));
};
//# sourceMappingURL=ToolResultDisplay.js.map