import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import Spinner from 'ink-spinner';
import { memo, useRef } from 'react';
import { useNonInteractiveRender } from '../hooks/useNonInteractiveRender.js';
import { useTerminalWidth } from '../hooks/useTerminalWidth.js';
import { useTheme } from '../hooks/useTheme.js';
import { wrapWithTrimmedContinuations } from '../utils/text-wrapping.js';
import { calculateTokens } from '../utils/token-calculator.js';
/**
 * Lightweight streaming reasoning component. Shows the last N lines of
 * plain text to avoid expensive markdown parsing and terminal reflow
 * on every token update. The final AssistantReasoning handles full rendering.
 */
export default memo(function StreamingReasoning({ reasoning, expand, }) {
    // Snapshot the wall clock on first render so tok/s measures streaming
    // throughput rather than request-send-to-now (which over-counts the
    // pre-first-token latency for reasoning models).
    const startRef = useRef(Date.now());
    const startTime = startRef.current;
    const { colors } = useTheme();
    const boxWidth = useTerminalWidth();
    const nonInteractive = useNonInteractiveRender();
    const textWidth = boxWidth - 3;
    // Only show the tail of the content to keep the render small
    // and avoid off-screen reflow that causes iTerm2 flickering.
    const MAX_LINES = 12;
    const wrapped = wrapWithTrimmedContinuations(reasoning.trimEnd(), textWidth);
    const lines = wrapped.split('\n');
    const truncated = lines.length > MAX_LINES;
    const visibleLines = truncated ? lines.slice(-MAX_LINES) : lines;
    const displayText = visibleLines.join('\n');
    const tokens = calculateTokens(reasoning);
    const elapsedSec = (Date.now() - startTime) / 1000;
    const tokPerSec = elapsedSec > 0.1 ? (tokens / elapsedSec).toFixed(1) : '—';
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 2, children: [_jsxs(Box, { children: [_jsxs(Text, { color: colors.tool, children: ['\u2699', " Thinking", _jsx(Spinner, { type: "simpleDots" })] }), expand ? (_jsxs(Text, { children: ['  ', "~", tokens.toLocaleString(), " tokens \u00B7 ", tokPerSec, " tok/s"] })) : nonInteractive ? null : (_jsxs(Text, { color: colors.secondary, children: ['  ', "ctrl+r to expand"] }))] }), expand && (_jsxs(Box, { flexDirection: "column", children: [truncated && _jsx(Text, { color: colors.secondary, children: "\u2026" }), _jsx(Text, { color: colors.secondary, italic: true, children: displayText })] }))] }));
});
//# sourceMappingURL=streaming-reasoning.js.map