import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useState, useEffect } from "react";
import { Text, Box } from "ink";
import { useTheme } from "../theme/theme.js";
function formatThinkingDuration(ms) {
    const totalSec = Math.round(ms / 1000);
    if (totalSec < 60)
        return `${totalSec}s`;
    const min = Math.floor(totalSec / 60);
    const sec = totalSec % 60;
    return sec > 0 ? `${min}m ${sec}s` : `${min}m`;
}
const ACCENT_COLOR = "#818cf8";
const BORDER_COLOR = "#4b5563";
export const ThinkingBlock = React.memo(function ThinkingBlock({ text, durationMs, streaming = false, defaultCollapsed = true, }) {
    const theme = useTheme();
    const [collapsed, setCollapsed] = useState(!streaming && defaultCollapsed);
    // When streaming finishes (streaming goes from true to false), auto-collapse
    const [wasStreaming, setWasStreaming] = useState(streaming);
    useEffect(() => {
        if (wasStreaming && !streaming) {
            setCollapsed(true);
        }
        setWasStreaming(streaming);
    }, [streaming, wasStreaming]);
    if (!text)
        return null;
    const durationLabel = durationMs != null && durationMs > 0 ? formatThinkingDuration(durationMs) : null;
    const headerText = streaming
        ? durationLabel
            ? `Thinking... (${durationLabel})`
            : "Thinking..."
        : durationLabel
            ? `Thought for ${durationLabel}`
            : "Thought";
    // Never show content while streaming — it causes scroll fighting in the
    // live area because every reveal tick re-renders a growing block.  The full
    // text is available once the block moves to Static history (collapsed).
    const showContent = !streaming && !collapsed;
    const chevron = showContent ? " ▼" : " ▶";
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Box, { children: [_jsx(Text, { color: ACCENT_COLOR, children: "💭 " }), _jsx(Text, { color: theme.textMuted, italic: true, children: headerText }), !streaming && _jsx(Text, { color: theme.textDim, children: chevron })] }), showContent && (_jsx(Box, { marginLeft: 1, borderStyle: "single", borderLeft: true, borderRight: false, borderTop: false, borderBottom: false, borderLeftColor: streaming ? ACCENT_COLOR : BORDER_COLOR, paddingLeft: 1, children: _jsx(Text, { color: theme.textDim, wrap: "wrap", children: text }) }))] }));
});
//# sourceMappingURL=ThinkingBlock.js.map