import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import { memo, useMemo } from 'react';
import { useNonInteractiveRender } from '../hooks/useNonInteractiveRender.js';
import { useTerminalWidth } from '../hooks/useTerminalWidth.js';
import { useTheme } from '../hooks/useTheme.js';
import { parseMarkdown } from '../markdown-parser/index.js';
import { wrapWithTrimmedContinuations } from '../utils/text-wrapping.js';
import { calculateTokens } from '../utils/token-calculator.js';
// Indent applied to the expanded body so the "⚙ Thought" header acts as a
// section header with its body (and any tool summary that follows) grouped
// beneath it. Keep in sync with the marginLeft used in
// displayCompactCountsSummary.
const EXPANDED_INDENT = 2;
export default memo(function AssistantReasoning({ reasoning, expand, }) {
    const { colors } = useTheme();
    const boxWidth = useTerminalWidth();
    const nonInteractive = useNonInteractiveRender();
    const tokens = calculateTokens(reasoning);
    const effectiveWidth = Math.max(1, boxWidth - EXPANDED_INDENT);
    // Render markdown to terminal-formatted text
    // Pre-wrap to avoid Ink's trim:false leaving leading spaces on wrapped lines
    const renderedMessage = useMemo(() => {
        try {
            // Reasoning should be rendered subtly, so render markdown with single color
            const mutedColors = {
                text: colors.secondary,
                primary: colors.secondary,
                secondary: colors.secondary,
                success: colors.secondary,
                error: colors.secondary,
                warning: colors.secondary,
                info: colors.secondary,
                tool: colors.secondary,
            };
            const parsed = parseMarkdown(reasoning, mutedColors, effectiveWidth).trimEnd();
            return wrapWithTrimmedContinuations(parsed, effectiveWidth);
        }
        catch {
            // Fallback to plain text if markdown parsing fails
            return wrapWithTrimmedContinuations(reasoning.trimEnd(), effectiveWidth);
        }
    }, [reasoning, colors, effectiveWidth]);
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Box, { children: [_jsxs(Text, { color: colors.tool, children: ['\u2699', " Thought"] }), !expand && !nonInteractive && (_jsxs(Text, { color: colors.secondary, children: ['  ', "ctrl+r to expand"] }))] }), expand && (_jsxs(Box, { flexDirection: "column", marginLeft: EXPANDED_INDENT, children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: colors.secondary, italic: true, children: renderedMessage }) }), _jsx(Box, { children: _jsxs(Text, { color: colors.secondary, children: ["~", tokens.toLocaleString(), " tokens", ' '] }) })] }))] }));
});
//# sourceMappingURL=assistant-reasoning.js.map