import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { Text } from 'ink';
const DEFAULT_MAX_VISIBLE_LINES = 120;
const DEFAULT_MAX_LINE_LENGTH = 160;
// Owns terminal-safe unified diff rendering for cli-v2 surfaces. It does not
// decide which diffs belong in conversation history or live review mode.
export function DiffPatchBlock({ id, maxLineLength = DEFAULT_MAX_LINE_LENGTH, maxVisibleLines = DEFAULT_MAX_VISIBLE_LINES, patch, truncated = false, }) {
    const rendered = preparePatchLines({ maxLineLength, maxVisibleLines, patch });
    return (_jsxs(_Fragment, { children: [rendered.lines.map((line, index) => (_jsx(Text, { color: resolveDiffLineColor(line), dimColor: isDiffHeaderLine(line), children: line }, `${id}:${index}`))), truncated || rendered.truncated ? _jsx(Text, { color: "yellow", children: "Diff preview truncated." }) : null] }));
}
function preparePatchLines({ maxLineLength, maxVisibleLines, patch, }) {
    const rawLines = patch.split('\n');
    const visibleLines = rawLines.slice(0, maxVisibleLines).map((line) => truncateLine(line, maxLineLength));
    return {
        lines: visibleLines,
        truncated: rawLines.length > visibleLines.length,
    };
}
function truncateLine(line, maxLineLength) {
    if (line.length <= maxLineLength) {
        return line;
    }
    return `${line.slice(0, maxLineLength - 3)}...`;
}
function resolveDiffLineColor(line) {
    if (line.startsWith('@@')) {
        return 'cyan';
    }
    if (line.startsWith('+') && !line.startsWith('+++')) {
        return 'green';
    }
    if (line.startsWith('-') && !line.startsWith('---')) {
        return 'red';
    }
    return undefined;
}
function isDiffHeaderLine(line) {
    return line.startsWith('diff --git') || line.startsWith('index ') || line.startsWith('---') || line.startsWith('+++');
}
//# sourceMappingURL=DiffPatchBlock.js.map