const require_splitTextByLine = require('../chokidar/dist/esm/utils/splitTextByLine.cjs');

//#region ../@intlayer/cli/dist/esm/utils/calculateChunks.mjs
const DEFAULT_MAX_CHARS_PER_CHUNK = 800;
const DEFAULT_OVERLAP_CHARS = 0;
const chunkText = (text, maxCharsPerChunk = DEFAULT_MAX_CHARS_PER_CHUNK, overlapChars = DEFAULT_OVERLAP_CHARS) => {
	if (maxCharsPerChunk <= 0) throw new Error("maxCharsPerChunk must be greater than 0");
	const splittedText = require_splitTextByLine.splitTextByLines(text);
	const lines = [];
	let charStartAcc = 0;
	splittedText.forEach((line, index) => {
		lines.push({
			content: line,
			lineStart: index,
			lineLength: 1,
			charStart: charStartAcc,
			charLength: line.length
		});
		charStartAcc += line.length;
	});
	const splittedLines = lines.reduce((acc, line) => {
		if (line.content.length > maxCharsPerChunk) {
			acc.push(line);
			return acc;
		}
		if (acc.length === 0) {
			acc.push(line);
			return acc;
		}
		const lastChunk = acc[acc.length - 1];
		if (lastChunk.content.length + line.content.length > maxCharsPerChunk) {
			acc.push(line);
			return acc;
		}
		const combinedContent = lastChunk.content + line.content;
		const updatedChunk = {
			content: combinedContent,
			lineStart: lastChunk.lineStart,
			lineLength: lastChunk.lineLength + line.lineLength,
			charStart: lastChunk.charStart,
			charLength: combinedContent.length
		};
		acc[acc.length - 1] = updatedChunk;
		return acc;
	}, []).flatMap((line) => {
		const chunk = [];
		if (line.content.length <= maxCharsPerChunk) {
			chunk.push(line);
			return chunk;
		}
		for (let i = 0; i < line.content.length; i += maxCharsPerChunk) {
			const slicedContent = line.content.slice(i, i + maxCharsPerChunk);
			chunk.push({
				content: slicedContent,
				lineStart: line.lineStart,
				lineLength: 1,
				charStart: line.charStart + i,
				charLength: slicedContent.length
			});
		}
		return chunk;
	});
	if (overlapChars === 0) return splittedLines;
	const overlapChunks = splittedLines.length > 0 ? [splittedLines[0]] : [];
	for (let i = 1; i < splittedLines.length; i++) {
		const previousChunk = splittedLines[i - 1];
		const chunk = splittedLines[i];
		const overlapContent = previousChunk.content.slice(-overlapChars);
		const overlapLineNb = require_splitTextByLine.splitTextByLines(overlapContent).length;
		const overlapContentWithoutPartialLine = overlapContent.slice(overlapLineNb > 1 ? overlapContent.indexOf("\n") + 1 : 0, overlapContent.length);
		const newContent = overlapContentWithoutPartialLine + chunk.content;
		const newLineLength = require_splitTextByLine.splitTextByLines(newContent).length;
		const lineDiff = chunk.lineLength - newLineLength;
		const overlappedChunk = {
			content: newContent,
			lineStart: chunk.lineStart + lineDiff,
			lineLength: chunk.lineLength - lineDiff,
			charStart: chunk.charStart - overlapContentWithoutPartialLine.length,
			charLength: newContent.length
		};
		overlapChunks.push(overlappedChunk);
	}
	return overlapChunks;
};

//#endregion
exports.chunkText = chunkText;
//# sourceMappingURL=calculateChunks.cjs.map