import { jsx as _jsx } from "react/jsx-runtime";
import { Box, Text } from "ink";
const DEFAULT_MAX_USER_PROMPT_CHARS = 10_000;
const DEFAULT_USER_PROMPT_HEAD_CHARS = 2_500;
const DEFAULT_USER_PROMPT_TAIL_CHARS = 2_500;
export function UserPromptMessage(props) {
    const displayText = formatUserPromptText(props.text);
    const lines = displayText.split(/\r?\n/);
    return (_jsx(Box, { flexDirection: "column", children: lines.map((line, lineIndex) => (_jsx(Text, { children: splitPromptSegments(line || " ").map((segment, segmentIndex) => (_jsx(Text, { bold: segment.kind === "command", color: promptSegmentColor(segment.kind), children: segment.text }, `${segmentIndex}:${segment.kind}:${segment.text.slice(0, 16)}`))) }, `${lineIndex}:${line.slice(0, 24)}`))) }));
}
export function formatUserPromptText(text, options = {}) {
    const value = text || " ";
    const maxChars = options.maxChars ?? DEFAULT_MAX_USER_PROMPT_CHARS;
    if (value.length <= maxChars)
        return value;
    const headChars = Math.max(1, options.headChars ?? DEFAULT_USER_PROMPT_HEAD_CHARS);
    const tailChars = Math.max(1, options.tailChars ?? DEFAULT_USER_PROMPT_TAIL_CHARS);
    const head = value.slice(0, headChars);
    const tail = value.slice(-tailChars);
    const hidden = value.slice(headChars, Math.max(headChars, value.length - tailChars));
    const hiddenLines = countNewlines(hidden);
    const lineLabel = hiddenLines === 1 ? "line" : "lines";
    return `${head}\n... +${hiddenLines} hidden ${lineLabel} ...\n${tail}`;
}
export function splitPromptSegments(line) {
    const segments = [];
    const pattern = /(^|\s)(\/[A-Za-z][\w-]*|@[^\s]+)/g;
    let cursor = 0;
    for (const match of line.matchAll(pattern)) {
        const index = match.index ?? 0;
        const prefix = match[1] ?? "";
        const token = match[2] ?? "";
        const tokenStart = index + prefix.length;
        if (tokenStart > cursor) {
            segments.push({ kind: "text", text: line.slice(cursor, tokenStart) });
        }
        segments.push({ kind: token.startsWith("/") ? "command" : "mention", text: token });
        cursor = tokenStart + token.length;
    }
    if (cursor < line.length) {
        segments.push({ kind: "text", text: line.slice(cursor) });
    }
    return segments.length > 0 ? segments : [{ kind: "text", text: line }];
}
function promptSegmentColor(kind) {
    if (kind === "command")
        return "yellow";
    if (kind === "mention")
        return "cyan";
    return undefined;
}
function countNewlines(value) {
    return (value.match(/\n/g) ?? []).length;
}
