
//#region ../@intlayer/cli/dist/esm/translation-alignment/segmentDocument.mjs
const isBlankLine = (line) => line.trim().length === 0;
const isFencedCodeDelimiter = (line) => /^\s*```/.test(line);
const isHeading = (line) => /^\s*#{1,6}\s+/.test(line);
const isHorizontalRule = (line) => /^(\s*[-*_]){3,}\s*$/.test(line);
const isListItem = (line) => /^\s*([-*+]\s+|\d+\.[\t\s]+)/.test(line);
const isBlockquote = (line) => /^\s*>\s?/.test(line);
const isTableLike = (line) => /\|/.test(line) && !isCodeFenceStart(line);
const isCodeFenceStart = (line) => /^\s*```/.test(line);
const trimTrailingNewlines = (text) => text.replace(/\n+$/g, "\n");
const segmentDocument = (text) => {
	const lines = text.split("\n");
	const blocks = [];
	let index = 0;
	while (index < lines.length) {
		const startIndex = index;
		const currentLine = lines[index];
		if (isFencedCodeDelimiter(currentLine)) {
			const contentLines = [currentLine];
			index += 1;
			while (index < lines.length && !isFencedCodeDelimiter(lines[index])) {
				contentLines.push(lines[index]);
				index += 1;
			}
			if (index < lines.length) {
				contentLines.push(lines[index]);
				index += 1;
			}
			blocks.push({
				type: "code_block",
				content: `${trimTrailingNewlines(contentLines.join("\n"))}\n`,
				lineStart: startIndex + 1,
				lineEnd: index
			});
			continue;
		}
		if (isHorizontalRule(currentLine)) {
			blocks.push({
				type: "horizontal_rule",
				content: `${currentLine}\n`,
				lineStart: startIndex + 1,
				lineEnd: startIndex + 1
			});
			index += 1;
			continue;
		}
		if (isHeading(currentLine)) {
			blocks.push({
				type: "heading",
				content: `${currentLine}\n`,
				lineStart: startIndex + 1,
				lineEnd: startIndex + 1
			});
			index += 1;
			continue;
		}
		if (isListItem(currentLine)) {
			const contentLines = [];
			while (index < lines.length && (isListItem(lines[index]) || !isBlankLine(lines[index]) && /^\s{2,}/.test(lines[index]))) {
				contentLines.push(lines[index]);
				index += 1;
			}
			blocks.push({
				type: "list_item",
				content: `${trimTrailingNewlines(contentLines.join("\n"))}\n`,
				lineStart: startIndex + 1,
				lineEnd: index
			});
			continue;
		}
		if (isBlockquote(currentLine)) {
			const contentLines = [];
			while (index < lines.length && (isBlockquote(lines[index]) || !isBlankLine(lines[index]))) {
				contentLines.push(lines[index]);
				index += 1;
			}
			blocks.push({
				type: "blockquote",
				content: `${trimTrailingNewlines(contentLines.join("\n"))}\n`,
				lineStart: startIndex + 1,
				lineEnd: index
			});
			continue;
		}
		if (isTableLike(currentLine)) {
			const contentLines = [];
			while (index < lines.length && /\|/.test(lines[index]) && !isBlankLine(lines[index])) {
				contentLines.push(lines[index]);
				index += 1;
			}
			blocks.push({
				type: "table",
				content: `${trimTrailingNewlines(contentLines.join("\n"))}\n`,
				lineStart: startIndex + 1,
				lineEnd: index
			});
			continue;
		}
		if (!isBlankLine(currentLine)) {
			const contentLines = [];
			while (index < lines.length && !isBlankLine(lines[index])) {
				if (isHeading(lines[index]) || isFencedCodeDelimiter(lines[index]) || isHorizontalRule(lines[index]) || isListItem(lines[index]) || isBlockquote(lines[index]) || isTableLike(lines[index])) break;
				contentLines.push(lines[index]);
				index += 1;
			}
			if (index < lines.length && isBlankLine(lines[index])) {
				contentLines.push(lines[index]);
				index += 1;
			}
			blocks.push({
				type: "paragraph",
				content: `${trimTrailingNewlines(contentLines.join("\n"))}\n`,
				lineStart: startIndex + 1,
				lineEnd: index
			});
			continue;
		}
		blocks.push({
			type: "unknown",
			content: `${currentLine}\n`,
			lineStart: startIndex + 1,
			lineEnd: startIndex + 1
		});
		index += 1;
	}
	return blocks;
};

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