/**
 * Incremental syntax highlighting for the write tool's streaming content.
 *
 * As the LLM streams file content, we incrementally highlight new lines
 * (single-line highlighting) and periodically re-highlight the first N lines
 * with full-context highlighting for better accuracy at the file's head.
 */
import { getLanguageFromPath, highlightCode } from '../../theme/theme.js';
import { normalizeToolText } from './tool-execution-types.js';
/** Number of leading lines re-highlighted with full-context on each incremental update. */
const WRITE_PARTIAL_FULL_HIGHLIGHT_LINES = 50;
function highlightSingleLine(line, lang) {
    const highlighted = highlightCode(line, lang);
    return highlighted[0] ?? '';
}
function refreshWriteHighlightPrefix(cache) {
    const prefixCount = Math.min(WRITE_PARTIAL_FULL_HIGHLIGHT_LINES, cache.normalizedLines.length);
    if (prefixCount === 0) {
        return cache;
    }
    const highlightedLines = [...cache.highlightedLines];
    const prefixSource = cache.normalizedLines.slice(0, prefixCount).join('\n');
    const prefixHighlighted = highlightCode(prefixSource, cache.lang);
    for (let i = 0; i < prefixCount; i++) {
        highlightedLines[i] =
            prefixHighlighted[i] ?? highlightSingleLine(cache.normalizedLines[i] ?? '', cache.lang);
    }
    return {
        ...cache,
        highlightedLines
    };
}
/** Build a complete highlight cache from scratch for the given file content. */
export function buildWriteHighlightCache(rawPath, fileContent) {
    const lang = rawPath ? getLanguageFromPath(rawPath) : undefined;
    if (!lang) {
        return undefined;
    }
    const normalized = normalizeToolText(fileContent);
    return {
        rawPath,
        lang,
        rawContent: fileContent,
        normalizedLines: normalized.split('\n'),
        highlightedLines: highlightCode(normalized, lang)
    };
}
/**
 * Incrementally update the highlight cache with appended content.
 * Falls back to a full rebuild if the content diverged from the cache.
 */
export function updateWriteHighlightCacheIncrementally(current, rawPath, fileContent) {
    const lang = rawPath ? getLanguageFromPath(rawPath) : undefined;
    if (!lang) {
        return undefined;
    }
    if (!current || current.lang !== lang || current.rawPath !== rawPath) {
        return buildWriteHighlightCache(rawPath, fileContent);
    }
    if (!fileContent.startsWith(current.rawContent)) {
        return buildWriteHighlightCache(rawPath, fileContent);
    }
    if (fileContent.length === current.rawContent.length) {
        return current;
    }
    const deltaRaw = fileContent.slice(current.rawContent.length);
    const deltaNormalized = normalizeToolText(deltaRaw);
    const normalizedLines = [...current.normalizedLines];
    const highlightedLines = [...current.highlightedLines];
    if (normalizedLines.length === 0) {
        normalizedLines.push('');
        highlightedLines.push('');
    }
    const segments = deltaNormalized.split('\n');
    const lastIndex = normalizedLines.length - 1;
    normalizedLines[lastIndex] += segments[0];
    highlightedLines[lastIndex] = highlightSingleLine(normalizedLines[lastIndex], lang);
    for (let i = 1; i < segments.length; i++) {
        normalizedLines.push(segments[i]);
        highlightedLines.push(highlightSingleLine(segments[i], lang));
    }
    return refreshWriteHighlightPrefix({
        rawPath,
        lang,
        rawContent: fileContent,
        normalizedLines,
        highlightedLines
    });
}
//# sourceMappingURL=tool-execution-write-highlight.js.map