import { jsx as _jsx } from "react/jsx-runtime";
import React, { useMemo } from "react";
import { Text, Box, useStdout } from "ink";
import { renderInlineMarkdownToAnsi, renderMarkdownToAnsiLines, wrapAnsiMarkdownLine, } from "../utils/markdown-renderer.js";
import { useTheme } from "../theme/theme.js";
function RenderAnsiLines({ lines }) {
    return (_jsx(Box, { flexDirection: "column", children: lines.map((line, index) => line.length === 0 ? (_jsx(Box, { height: 1 }, index)) : (_jsx(Text, { wrap: "truncate-end", children: line }, index))) }));
}
export const Markdown = React.memo(function Markdown({ children, width: explicitWidth, compact = false, renderMarkdown = true, isPending = false, availableTerminalHeight, }) {
    const { stdout } = useStdout();
    const theme = useTheme();
    const terminalWidth = explicitWidth ?? Math.max(40, (stdout?.columns || 80) - 4);
    const text = compact ? children.trim() : children;
    const renderedLines = useMemo(() => renderMarkdownToAnsiLines({
        text,
        theme,
        width: terminalWidth,
        isPending,
        availableTerminalHeight,
        renderMarkdown,
    }), [availableTerminalHeight, isPending, renderMarkdown, terminalWidth, text, theme]);
    // When clamped, keep the TAIL of the response — but never let the slice
    // start on a paragraph-break blank line. The ⏺ prefix renders in a sibling
    // box aligned to the first row of this column, so a leading blank row leaves
    // the dot floating one row above the text (alignment varies with where the
    // slice boundary lands).
    let visibleLines = availableTerminalHeight !== undefined && renderedLines.length > availableTerminalHeight
        ? renderedLines.slice(-availableTerminalHeight)
        : renderedLines;
    while (visibleLines.length > 0 && visibleLines[0] === "") {
        visibleLines = visibleLines.slice(1);
    }
    if (!text || visibleLines.length === 0)
        return null;
    return (_jsx(Box, { flexDirection: "column", width: terminalWidth, flexShrink: 1, children: _jsx(RenderAnsiLines, { lines: visibleLines }) }));
});
export const StreamingMarkdown = React.memo(function StreamingMarkdown({ children, width, compact = false, }) {
    return (_jsx(Markdown, { width: width, compact: compact, isPending: true, children: children }));
});
export { renderInlineMarkdownToAnsi, wrapAnsiMarkdownLine };
//# sourceMappingURL=Markdown.js.map