function splitLinesWithEndings(content) {
    return content.match(/[^\n]*\n|[^\n]+/g) ?? [];
}
function getLineSpans(content) {
    let offset = 0;
    return splitLinesWithEndings(content).map((line) => {
        const span = { start: offset, end: offset + line.length };
        offset = span.end;
        return span;
    });
}
function getReplacementLineRange(lines, replacement) {
    const replacementStart = replacement.matchIndex;
    const replacementEnd = replacement.matchIndex + replacement.matchLength;
    let startLine = -1;
    for (let i = 0; i < lines.length; i++) {
        const line = lines[i];
        if (replacementStart >= line.start && replacementStart < line.end) {
            startLine = i;
            break;
        }
    }
    if (startLine === -1) {
        throw new Error("Replacement range is outside the base content.");
    }
    let endLine = startLine;
    while (endLine < lines.length && lines[endLine].end < replacementEnd) {
        endLine++;
    }
    if (endLine >= lines.length) {
        throw new Error("Replacement range is outside the base content.");
    }
    return { startLine, endLine: endLine + 1 };
}
export function applyReplacements(content, replacements, offset = 0) {
    let result = content;
    for (let i = replacements.length - 1; i >= 0; i--) {
        const replacement = replacements[i];
        const matchIndex = replacement.matchIndex - offset;
        result =
            result.substring(0, matchIndex) + replacement.newText + result.substring(matchIndex + replacement.matchLength);
    }
    return result;
}
/**
 * Apply replacements matched against `baseContent` to `originalContent` while
 * preserving unchanged line blocks from the original.
 *
 * This is useful when `baseContent` is a normalized view of the original. Each
 * replacement is widened to the lines it actually touches, those touched lines
 * are rewritten from the normalized base, and all other lines are copied back
 * from `originalContent`. The actual replacement ranges drive preservation so
 * duplicate normalized lines cannot be aligned to the wrong occurrence.
 */
export function applyReplacementsPreservingUnchangedLines(originalContent, baseContent, replacements) {
    const originalLines = splitLinesWithEndings(originalContent);
    const baseLines = getLineSpans(baseContent);
    if (originalLines.length !== baseLines.length) {
        throw new Error("Cannot preserve unchanged lines because the base content has a different line count.");
    }
    const groups = [];
    const sortedReplacements = [...replacements].sort((a, b) => a.matchIndex - b.matchIndex);
    for (const replacement of sortedReplacements) {
        const range = getReplacementLineRange(baseLines, replacement);
        const current = groups[groups.length - 1];
        if (current && range.startLine < current.endLine) {
            current.endLine = Math.max(current.endLine, range.endLine);
            current.replacements.push(replacement);
            continue;
        }
        groups.push({ ...range, replacements: [replacement] });
    }
    let originalLineIndex = 0;
    let result = "";
    for (const group of groups) {
        result += originalLines.slice(originalLineIndex, group.startLine).join("");
        const groupStartOffset = baseLines[group.startLine].start;
        const groupEndOffset = baseLines[group.endLine - 1].end;
        result += applyReplacements(baseContent.slice(groupStartOffset, groupEndOffset), group.replacements, groupStartOffset);
        originalLineIndex = group.endLine;
    }
    result += originalLines.slice(originalLineIndex).join("");
    return result;
}
//# sourceMappingURL=edit-diff-preserve.js.map