/**
 * Plain-text diff rendering for LLM consumption.
 * Extracted from the TUI interactive mode diff renderer, with all ANSI
 * styling removed — the output is plain text suitable for LLM input.
 */
import * as Diff from "diff";
/**
 * Parse diff line to extract prefix, line number, and content.
 * Format: "+123 content" or "-123 content" or " 123 content" or "     ..."
 */
function parseDiffLine(line) {
    const match = line.match(/^([+-\s])(\s*\d*)\s(.*)$/);
    if (!match)
        return null;
    return { prefix: match[1], lineNum: match[2], content: match[3] };
}
function replaceTabs(text) {
    return text.replace(/\t/g, "   ");
}
function renderIntraLineDiff(oldContent, newContent) {
    const wordDiff = Diff.diffWords(oldContent, newContent);
    let removedLine = "";
    let addedLine = "";
    let isFirstRemoved = true;
    let isFirstAdded = true;
    for (const part of wordDiff) {
        if (part.removed) {
            let value = part.value;
            if (isFirstRemoved) {
                const leadingWs = value.match(/^(\s*)/)?.[1] || "";
                value = value.slice(leadingWs.length);
                removedLine += leadingWs;
                isFirstRemoved = false;
            }
            if (value) {
                removedLine += `<REMOVED>${value}</REMOVED>`;
            }
        }
        else if (part.added) {
            let value = part.value;
            if (isFirstAdded) {
                const leadingWs = value.match(/^(\s*)/)?.[1] || "";
                value = value.slice(leadingWs.length);
                addedLine += leadingWs;
                isFirstAdded = false;
            }
            if (value) {
                addedLine += `<ADDED>${value}</ADDED>`;
            }
        }
        else {
            removedLine += part.value;
            addedLine += part.value;
        }
    }
    return { removedLine, addedLine };
}
/**
 * Render a diff string as plain text with markers.
 * - Context lines: unchanged
 * - Removed lines: prefixed with "-"
 * - Added lines: prefixed with "+"
 */
export function renderDiff(diffText, _options = {}) {
    const lines = diffText.split("\n");
    const result = [];
    let i = 0;
    while (i < lines.length) {
        const line = lines[i];
        const parsed = parseDiffLine(line);
        if (!parsed) {
            result.push(line);
            i++;
            continue;
        }
        if (parsed.prefix === "-") {
            const removedLines = [];
            while (i < lines.length) {
                const p = parseDiffLine(lines[i]);
                if (!p || p.prefix !== "-")
                    break;
                removedLines.push({ lineNum: p.lineNum, content: p.content });
                i++;
            }
            const addedLines = [];
            while (i < lines.length) {
                const p = parseDiffLine(lines[i]);
                if (!p || p.prefix !== "+")
                    break;
                addedLines.push({ lineNum: p.lineNum, content: p.content });
                i++;
            }
            if (removedLines.length === 1 && addedLines.length === 1) {
                const removed = removedLines[0];
                const added = addedLines[0];
                const { removedLine, addedLine } = renderIntraLineDiff(replaceTabs(removed.content), replaceTabs(added.content));
                result.push(`-${removed.lineNum} ${removedLine}`);
                result.push(`+${added.lineNum} ${addedLine}`);
            }
            else {
                for (const removed of removedLines) {
                    result.push(`-${removed.lineNum} ${replaceTabs(removed.content)}`);
                }
                for (const added of addedLines) {
                    result.push(`+${added.lineNum} ${replaceTabs(added.content)}`);
                }
            }
        }
        else if (parsed.prefix === "+") {
            result.push(`+${parsed.lineNum} ${replaceTabs(parsed.content)}`);
            i++;
        }
        else {
            result.push(` ${parsed.lineNum} ${replaceTabs(parsed.content)}`);
            i++;
        }
    }
    return result.join("\n");
}
