import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } 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 { parseMarkdownParts } from '../markdown-parser/index.js';
import { wrapWithTrimmedContinuations } from '../utils/text-wrapping.js';
import { calculateTokens } from '../utils/token-calculator.js';
export function AssistantMessageBox({ text, truncated, }) {
    const { colors } = useTheme();
    const boxWidth = useTerminalWidth();
    return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, backgroundColor: colors.base, width: boxWidth, padding: 1, borderStyle: "bold", borderLeft: true, borderRight: false, borderTop: false, borderBottom: false, borderLeftColor: colors.secondary, children: [truncated && _jsx(Text, { children: "\u2026" }), _jsx(Text, { children: text })] }));
}
export default memo(function AssistantMessage({ message, model, }) {
    const { colors } = useTheme();
    const boxWidth = useTerminalWidth();
    const nonInteractive = useNonInteractiveRender();
    const tokens = calculateTokens(message);
    // Inner text width: outer width minus left border (1) and padding (1 each side)
    const textWidth = nonInteractive ? boxWidth : boxWidth - 3;
    // Render markdown into segments: text parts (rendered inside the bordered box)
    // and code parts (rendered without a border so they can be copied cleanly).
    // For non-interactive mode we join all parts back into a flat string.
    // Pre-wrap text parts to avoid Ink's trim:false leaving leading spaces on
    // wrapped lines. trim() removes leading/trailing whitespace.
    const renderedParts = useMemo(() => {
        try {
            const parts = parseMarkdownParts(message, colors, textWidth);
            return parts
                .map(part => {
                if (part.type === 'text') {
                    const trimmed = part.content.trim();
                    return trimmed
                        ? {
                            type: 'text',
                            content: wrapWithTrimmedContinuations(trimmed, textWidth),
                        }
                        : null;
                }
                return part; // code: keep as-is, no wrapping
            })
                .filter(Boolean);
        }
        catch {
            // Fallback to a single text part if parsing fails
            return [
                {
                    type: 'text',
                    content: wrapWithTrimmedContinuations(message.trim(), textWidth),
                },
            ];
        }
    }, [message, colors, textWidth]);
    // Non-interactive (`run`) mode: plain markdown text, no header/box/token
    // counter — keeps stdout output close to what a regular CLI would emit.
    if (nonInteractive) {
        const flatText = renderedParts.map(p => p?.content ?? '').join('\n');
        return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: _jsx(Text, { children: flatText }) }));
    }
    return (_jsxs(_Fragment, { children: [_jsx(Box, { marginBottom: 1, marginTop: 1, children: _jsxs(Text, { color: colors.info, bold: true, children: [model, ":"] }) }), renderedParts.map((part, index) => part?.type === 'text' ? (_jsx(AssistantMessageBox, { text: part.content }, index)) : (
            // Code blocks rendered without any border or margin so they can be
            // selected and copied cleanly from the terminal.
            _jsx(Box, { marginBottom: 1, children: _jsx(Text, { children: part?.content }) }, index))), _jsx(Box, { marginBottom: 2, children: _jsxs(Text, { color: colors.secondary, children: ["~", tokens.toLocaleString(), " tokens"] }) })] }));
});
//# sourceMappingURL=assistant-message.js.map