import * as Diff from "diff";
import { theme } from "../theme/theme.js";
/**
 * 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] };
}
/**
 * Replace tabs with spaces for consistent rendering.
 */
function replaceTabs(text) {
    return text.replace(/\t/g, "   ");
}
/**
 * Compute word-level diff and render with background highlights on changed parts.
 * Uses diffWords which groups whitespace with adjacent words for cleaner highlighting.
 * Strips leading whitespace from highlights to avoid highlighting indentation.
 */
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 += theme.bg("toolDiffRemovedWord", value);
            }
        }
        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 += theme.bg("toolDiffAddedWord", value);
            }
        }
        else {
            removedLine += part.value;
            addedLine += part.value;
        }
    }
    return { removedLine, addedLine };
}
/**
 * Render a diff string with colored lines and intra-line change highlighting.
 * - Context lines: dim/gray
 * - Removed lines: red, with background highlights on changed words
 * - Added lines: green, with background highlights on changed words
 */
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(theme.fg("toolDiffContext", 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(theme.fg("error", " - ") + theme.fg("toolDiffRemoved", `${removed.lineNum} ${removedLine}`));
                result.push(theme.fg("success", " + ") + theme.fg("toolDiffAdded", `${added.lineNum} ${addedLine}`));
            }
            else {
                for (const removed of removedLines) {
                    result.push(theme.fg("error", " - ") +
                        theme.fg("toolDiffRemoved", `${removed.lineNum} ${replaceTabs(removed.content)}`));
                }
                for (const added of addedLines) {
                    result.push(theme.fg("success", " + ") +
                        theme.fg("toolDiffAdded", `${added.lineNum} ${replaceTabs(added.content)}`));
                }
            }
        }
        else if (parsed.prefix === "+") {
            result.push(theme.fg("success", " + ") + theme.fg("toolDiffAdded", `${parsed.lineNum} ${replaceTabs(parsed.content)}`));
            i++;
        }
        else {
            result.push(theme.fg("dim", "   ") + theme.fg("toolDiffContext", `${parsed.lineNum} ${replaceTabs(parsed.content)}`));
            i++;
        }
    }
    return result.join("\n");
}
//# sourceMappingURL=diff.js.map