const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
const require_utils_calculateChunks = require('./calculateChunks.cjs');
let __intlayer_chokidar = require("@intlayer/chokidar");

//#region src/utils/mapChunksBetweenFiles.ts
/**
* Maps chunks from base file to corresponding chunks in updated file,
* handling insertions, deletions, and modifications properly.
*/
const mapChunksBetweenFiles = (baseFileContent, updatedFileContent, maxCharsPerChunk = 800, changedLines) => {
	const baseChunks = require_utils_calculateChunks.chunkText(baseFileContent, maxCharsPerChunk, 0);
	const baseLines = (0, __intlayer_chokidar.splitTextByLines)(baseFileContent);
	const updatedLines = (0, __intlayer_chokidar.splitTextByLines)(updatedFileContent);
	const lineMapping = createLineMapping(baseLines, updatedLines);
	return baseChunks.map((baseChunk) => {
		const mappedRange = mapLineRange(baseChunk.lineStart, baseChunk.lineLength, lineMapping);
		if (!mappedRange) return {
			baseChunk,
			updatedChunk: null,
			hasChanges: true
		};
		const updatedChunk = {
			lineStart: mappedRange.start,
			lineLength: mappedRange.length,
			charStart: 0,
			charLength: 0,
			content: extractLinesFromRange(updatedLines, mappedRange.start, mappedRange.length)
		};
		updatedChunk.charStart = getCharStartForLine(updatedFileContent, updatedChunk.lineStart);
		updatedChunk.charLength = updatedChunk.content.length;
		return {
			baseChunk,
			updatedChunk,
			hasChanges: determineIfChunkHasChanges(baseChunk, updatedChunk, changedLines)
		};
	});
};
/**
* Creates a mapping between line numbers in base file and updated file
* Returns a map where key = base line number, value = updated line number (or null if deleted)
*/
const createLineMapping = (baseLines, updatedLines) => {
	const mapping = /* @__PURE__ */ new Map();
	const dp = Array(baseLines.length + 1).fill(null).map(() => Array(updatedLines.length + 1).fill(0));
	for (let i$1 = 1; i$1 <= baseLines.length; i$1++) for (let j$1 = 1; j$1 <= updatedLines.length; j$1++) if (baseLines[i$1 - 1] === updatedLines[j$1 - 1]) dp[i$1][j$1] = dp[i$1 - 1][j$1 - 1] + 1;
	else dp[i$1][j$1] = Math.max(dp[i$1 - 1][j$1], dp[i$1][j$1 - 1]);
	let i = baseLines.length;
	let j = updatedLines.length;
	while (i > 0 || j > 0) if (i > 0 && j > 0 && baseLines[i - 1] === updatedLines[j - 1]) {
		mapping.set(i - 1, j - 1);
		i--;
		j--;
	} else if (i > 0 && (j === 0 || dp[i - 1][j] >= dp[i][j - 1])) {
		mapping.set(i - 1, null);
		i--;
	} else j--;
	return mapping;
};
/**
* Maps a line range from base file to updated file using the line mapping
*/
const mapLineRange = (baseStart, baseLength, lineMapping) => {
	const mappedLines = [];
	for (let i = baseStart; i < baseStart + baseLength; i++) {
		const mappedLine = lineMapping.get(i);
		if (mappedLine !== null && mappedLine !== void 0) mappedLines.push(mappedLine);
	}
	if (mappedLines.length === 0) return null;
	mappedLines.sort((a, b) => a - b);
	const start = mappedLines[0];
	return {
		start,
		length: mappedLines[mappedLines.length - 1] - start + 1
	};
};
/**
* Extracts lines from a range in the lines array
*/
const extractLinesFromRange = (lines, start, length) => {
	const endIndex = Math.min(start + length, lines.length);
	return lines.slice(start, endIndex).join("");
};
/**
* Gets the character position where a line starts in the text
*/
const getCharStartForLine = (text, lineNumber) => {
	const lines = (0, __intlayer_chokidar.splitTextByLines)(text);
	let charStart = 0;
	for (let i = 0; i < Math.min(lineNumber, lines.length); i++) charStart += lines[i].length;
	return charStart;
};
/**
* Determines if a chunk has changes based on git changed lines or content comparison
*/
const determineIfChunkHasChanges = (baseChunk, updatedChunk, changedLines) => {
	if (changedLines && changedLines.length > 0) return changedLines.some((line) => line >= updatedChunk.lineStart && line < updatedChunk.lineStart + updatedChunk.lineLength);
	return baseChunk.content !== updatedChunk.content;
};

//#endregion
exports.mapChunksBetweenFiles = mapChunksBetweenFiles;
//# sourceMappingURL=mapChunksBetweenFiles.cjs.map