import { jsx as _jsx } from "react/jsx-runtime";
import { AssistantTextMessage, formatAssistantText } from "./AssistantTextMessage.js";
import { cellWidth } from "../prompt/promptViewport.js";
import { Markdown } from "./Markdown.js";
import { MessageRow } from "./MessageRow.js";
import { RateLimitMessage, isRateLimitText } from "./RateLimitMessage.js";
import { SystemAPIErrorMessage } from "./SystemAPIErrorMessage.js";
import { SystemTextMessage, formatSystemText } from "./SystemTextMessage.js";
import { ThinkingMessage, formatThinkingPreview } from "./ThinkingMessage.js";
import { ToolResultMessage } from "./ToolResultMessage.js";
import { ToolStartMessage } from "./ToolStartMessage.js";
import { UserPromptMessage, formatUserPromptText } from "./UserPromptMessage.js";
const DEFAULT_MAX_TRANSCRIPT_CHARS = 10_000;
const DEFAULT_TRANSCRIPT_HEAD_CHARS = 2_500;
const DEFAULT_TRANSCRIPT_TAIL_CHARS = 2_500;
export function TranscriptMessage(props) {
    const meta = transcriptRoleMeta(props.item.role);
    const text = formatTranscriptText(props.item);
    const metadata = hasTranscriptMetadata(props.item);
    return (_jsx(MessageRow, { meta: meta, metadata: metadata ? props.item : undefined, isToolLike: isToolLikeRole(props.item.role), width: props.width, children: props.item.role === "tool-start" ? (_jsx(ToolStartMessage, { text: text })) : props.item.role === "tool" ? (_jsx(ToolResultMessage, { text: text })) : props.item.role === "thinking" ? (_jsx(ThinkingMessage, { text: props.item.text, previewWidth: Math.max(24, (props.width ?? 120) - 6) })) : props.item.role === "system" ? (_jsx(SystemTextMessage, { text: text })) : props.item.role === "user" ? (_jsx(UserPromptMessage, { text: text })) : props.item.role === "assistant" ? (_jsx(AssistantTextMessage, { text: text })) : props.item.role === "error" && isRateLimitText(text) ? (_jsx(RateLimitMessage, { text: text })) : props.item.role === "error" ? (_jsx(SystemAPIErrorMessage, { error: text })) : (_jsx(Markdown, { dimColor: meta.dimBody, children: text })) }));
}
export function transcriptRoleMeta(role) {
    if (role === "user")
        return { label: "You", tone: "brand" };
    if (role === "assistant")
        return { label: "DeepSeekCode", tone: "success" };
    if (role === "tool-start")
        return { label: "tool", tone: "warning", dimBody: true };
    if (role === "tool")
        return { label: "tool", tone: "warning" };
    if (role === "thinking")
        return { label: "thinking", tone: "muted", dimBody: true };
    if (role === "error")
        return { label: "error", tone: "error" };
    if (role === "display")
        return { label: "display", tone: "muted", dimBody: true };
    return { label: "system", tone: "muted", dimBody: true };
}
export function estimateTranscriptRows(item, width = 80) {
    const textRows = formatTranscriptText(item).split(/\r?\n/).length;
    const wrapRows = estimateWrappedRows(formatTranscriptText(item), Math.max(16, width - 8));
    return 1 + Math.max(textRows, wrapRows) + (isToolLikeRole(item.role) ? 0 : 1) + (hasTranscriptMetadata(item) ? 1 : 0);
}
export function formatTranscriptText(item) {
    const text = item.text || " ";
    if (item.role === "user")
        return formatUserPromptText(text);
    if (item.role === "assistant")
        return formatAssistantText(truncateTranscriptText(text));
    if (item.role === "system")
        return formatSystemText(text);
    if (item.role === "thinking")
        return formatThinkingPreview(text);
    if (isToolLikeRole(item.role))
        return text.trim() || "tool result";
    return truncateTranscriptText(text);
}
function isToolLikeRole(role) {
    return role === "tool" || role === "tool-start";
}
export function hasTranscriptMetadata(item) {
    return item.role === "assistant" && Boolean(item.timestamp || item.model || item.streaming);
}
export function truncateTranscriptText(text, options = {}) {
    const maxChars = options.maxChars ?? DEFAULT_MAX_TRANSCRIPT_CHARS;
    if (text.length <= maxChars)
        return text;
    const headChars = Math.max(1, options.headChars ?? DEFAULT_TRANSCRIPT_HEAD_CHARS);
    const tailChars = Math.max(1, options.tailChars ?? DEFAULT_TRANSCRIPT_TAIL_CHARS);
    const head = text.slice(0, headChars);
    const tail = text.slice(-tailChars);
    const hidden = text.slice(headChars, Math.max(headChars, text.length - tailChars));
    const hiddenLines = countNewlines(hidden);
    const lineLabel = hiddenLines === 1 ? "line" : "lines";
    return `${head}\n... +${hiddenLines} hidden ${lineLabel} ...\n${tail}`;
}
function countNewlines(value) {
    return (value.match(/\n/g) ?? []).length;
}
function estimateWrappedRows(text, width) {
    return text
        .split(/\r?\n/)
        .reduce((sum, line) => sum + Math.max(1, Math.ceil(cellWidth(line) / Math.max(1, width))), 0);
}
