const require_splitTextByLine = require('./splitTextByLine.cjs');

//#region ../@intlayer/cli/dist/esm/chokidar/dist/esm/utils/getChunk.mjs
const getChunk = (text, options = {}) => {
	const { lineStart, lineLength, charStart, charLength } = options;
	if (lineStart === void 0 && lineLength === void 0 && charStart === void 0 && charLength === void 0) return text;
	const lines = require_splitTextByLine.splitTextByLines(text);
	const getCharIndexOfLineStart = (lineNumber) => {
		if (lineNumber <= 0) return 0;
		let idx = 0;
		for (let i = 0; i < Math.min(lineNumber, lines.length); i++) idx += lines[i].length;
		return idx;
	};
	const getCharIndexOfLineEnd = (lineNumber) => {
		if (lineNumber >= lines.length) return text.length;
		const line = lines[lineNumber];
		return getCharIndexOfLineStart(lineNumber) + line.length;
	};
	let effectiveStart = 0;
	let effectiveEnd = text.length;
	if (lineStart !== void 0) effectiveStart = Math.max(effectiveStart, getCharIndexOfLineStart(lineStart));
	if (charStart !== void 0) effectiveStart = Math.max(effectiveStart, charStart);
	if (lineLength !== void 0) {
		const endLine = (lineStart ?? 0) + lineLength - 1;
		effectiveEnd = Math.min(effectiveEnd, getCharIndexOfLineEnd(endLine));
	}
	if (charLength !== void 0) effectiveEnd = Math.min(effectiveEnd, (charStart ?? effectiveStart) + charLength);
	if (effectiveStart >= effectiveEnd) return "";
	return text.slice(effectiveStart, effectiveEnd);
};

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