import { jsx as _jsx } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import React from 'react';
import { Box, Text } from 'ink';
import { DiffRenderer } from './DiffRenderer.js';
import { MarkdownDisplay } from '../../utils/MarkdownDisplay.js';
import { AnsiOutputText, AnsiLineText } from '../AnsiOutput.js';
import { SlicingMaxSizedBox } from '../shared/SlicingMaxSizedBox.js';
import { theme } from '../../semantic-colors.js';
import { isSubagentProgress, } from '@google/gemini-cli-core';
import { useUIState } from '../../contexts/UIStateContext.js';
import { tryParseJSON } from '../../../utils/jsonoutput.js';
import { useAlternateBuffer } from '../../hooks/useAlternateBuffer.js';
import { Scrollable } from '../shared/Scrollable.js';
import { ScrollableList } from '../shared/ScrollableList.js';
import { SCROLL_TO_ITEM_END } from '../shared/VirtualizedList.js';
import { ACTIVE_SHELL_MAX_LINES } from '../../constants.js';
import { calculateToolContentMaxLines } from '../../utils/toolLayoutUtils.js';
import { SubagentProgressDisplay } from './SubagentProgressDisplay.js';
export const ToolResultDisplay = ({ resultDisplay, availableTerminalHeight, terminalWidth, renderOutputAsMarkdown = true, maxLines, hasFocus = false, overflowDirection = 'top', }) => {
    const { renderMarkdown } = useUIState();
    const isAlternateBuffer = useAlternateBuffer();
    const availableHeight = calculateToolContentMaxLines({
        availableTerminalHeight,
        isAlternateBuffer,
        maxLinesLimit: maxLines,
    });
    const combinedPaddingAndBorderWidth = 4;
    const childWidth = terminalWidth - combinedPaddingAndBorderWidth;
    const keyExtractor = React.useCallback((_, index) => index.toString(), []);
    const renderVirtualizedAnsiLine = React.useCallback(({ item }) => (_jsx(Box, { height: 1, overflow: "hidden", children: _jsx(AnsiLineText, { line: item }) })), []);
    if (!resultDisplay)
        return null;
    // 1. Early return for background tools (Todos)
    if (typeof resultDisplay === 'object' && 'todos' in resultDisplay) {
        // display nothing, as the TodoTray will handle rendering todos
        return null;
    }
    const renderContent = (contentData) => {
        // Check if string content is valid JSON and pretty-print it
        const prettyJSON = typeof contentData === 'string' ? tryParseJSON(contentData) : null;
        const formattedJSON = prettyJSON
            ? JSON.stringify(prettyJSON, null, 2)
            : null;
        let content;
        if (formattedJSON) {
            // Render pretty-printed JSON
            content = (_jsx(Text, { wrap: "wrap", color: theme.text.primary, children: formattedJSON }));
        }
        else if (isSubagentProgress(contentData)) {
            content = _jsx(SubagentProgressDisplay, { progress: contentData });
        }
        else if (typeof contentData === 'string' && renderOutputAsMarkdown) {
            content = (_jsx(MarkdownDisplay, { text: contentData, terminalWidth: childWidth, renderMarkdown: renderMarkdown, isPending: false }));
        }
        else if (typeof contentData === 'string' && !renderOutputAsMarkdown) {
            content = (_jsx(Text, { wrap: "wrap", color: theme.text.primary, children: contentData }));
        }
        else if (typeof contentData === 'object' && 'fileDiff' in contentData) {
            content = (_jsx(DiffRenderer, { diffContent: 
                // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
                contentData.fileDiff, 
                // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
                filename: contentData.fileName, availableTerminalHeight: availableHeight, terminalWidth: childWidth }));
        }
        else {
            const shouldDisableTruncation = isAlternateBuffer ||
                (availableTerminalHeight === undefined && maxLines === undefined);
            content = (_jsx(AnsiOutputText
            // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
            , { 
                // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
                data: contentData, availableTerminalHeight: isAlternateBuffer ? undefined : availableHeight, width: childWidth, maxLines: isAlternateBuffer ? undefined : maxLines, disableTruncation: shouldDisableTruncation }));
        }
        // Final render based on session mode
        if (isAlternateBuffer) {
            return (_jsx(Scrollable, { width: childWidth, maxHeight: maxLines ?? availableHeight, hasFocus: hasFocus, scrollToBottom: true, reportOverflow: true, children: content }));
        }
        return content;
    };
    // ASB Mode Handling (Interactive/Fullscreen)
    if (isAlternateBuffer) {
        // Virtualized path for large ANSI arrays
        if (Array.isArray(resultDisplay)) {
            const limit = maxLines ?? availableHeight ?? ACTIVE_SHELL_MAX_LINES;
            const listHeight = Math.min(
            // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
            resultDisplay.length, limit);
            return (_jsx(Box, { width: childWidth, flexDirection: "column", maxHeight: listHeight, children: _jsx(ScrollableList, { width: childWidth, 
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
                    data: resultDisplay, renderItem: renderVirtualizedAnsiLine, estimatedItemHeight: () => 1, keyExtractor: keyExtractor, initialScrollIndex: SCROLL_TO_ITEM_END, hasFocus: hasFocus }) }));
        }
        // Standard path for strings/diffs in ASB
        return (_jsx(Box, { width: childWidth, flexDirection: "column", children: renderContent(resultDisplay) }));
    }
    // Standard Mode Handling (History/Scrollback)
    // We use SlicingMaxSizedBox which includes MaxSizedBox for precision truncation + hidden labels
    return (_jsx(Box, { width: childWidth, flexDirection: "column", children: _jsx(SlicingMaxSizedBox, { data: resultDisplay, maxLines: maxLines, isAlternateBuffer: isAlternateBuffer, maxHeight: availableHeight, maxWidth: childWidth, overflowDirection: overflowDirection, children: (truncatedResultDisplay) => renderContent(truncatedResultDisplay) }) }));
};
//# sourceMappingURL=ToolResultDisplay.js.map