const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
const require__utils_asset = require('./_virtual/_utils_asset.cjs');
const require_translation_alignment_rebuildDocument = require('./translation-alignment/rebuildDocument.cjs');
const require_translation_alignment_pipeline = require('./translation-alignment/pipeline.cjs');
const require_utils_chunkInference = require('./utils/chunkInference.cjs');
const require_utils_fixChunkStartEndChars = require('./utils/fixChunkStartEndChars.cjs');
let __intlayer_chokidar = require("@intlayer/chokidar");
let __intlayer_config = require("@intlayer/config");
let node_path = require("node:path");
let __intlayer_core = require("@intlayer/core");
let node_fs = require("node:fs");
let node_fs_promises = require("node:fs/promises");
let __intlayer_types = require("@intlayer/types");

//#region src/reviewDocBlockAware.ts
/**
* Review a file using block-aware alignment.
* This approach:
* 1. Segments both English and French documents into semantic blocks
* 2. Aligns blocks using structure (special chars, numbers) and context
* 3. Detects which blocks changed, were added, or deleted
* 4. Only sends changed/new blocks to AI for translation
* 5. Handles reordering automatically
*/
const reviewFileBlockAware = async (baseFilePath, outputFilePath, locale, baseLocale, aiOptions, configOptions, customInstructions, changedLines) => {
	const configuration = (0, __intlayer_config.getConfiguration)(configOptions);
	const applicationLogger = (0, __intlayer_config.getAppLogger)(configuration);
	const englishText = await (0, node_fs_promises.readFile)(baseFilePath, "utf-8");
	const frenchText = await (0, node_fs_promises.readFile)(outputFilePath, "utf-8").catch(() => "");
	const basePrompt = require__utils_asset.readAsset("./prompts/REVIEW_PROMPT.md", "utf-8").replaceAll("{{localeName}}", `${(0, __intlayer_chokidar.formatLocale)(locale, false)}`).replaceAll("{{baseLocaleName}}", `${(0, __intlayer_chokidar.formatLocale)(baseLocale, false)}`).replace("{{applicationContext}}", aiOptions?.applicationContext ?? "-").replace("{{customInstructions}}", customInstructions ?? "-");
	const filePrefix = [(0, __intlayer_config.colon)(`${__intlayer_config.ANSIColors.GREY_DARK}[${(0, __intlayer_chokidar.formatPath)(baseFilePath)}${__intlayer_config.ANSIColors.GREY_DARK}] `, { colSize: 40 }), `→ ${__intlayer_config.ANSIColors.RESET}`].join("");
	const prefix = [(0, __intlayer_config.colon)(`${__intlayer_config.ANSIColors.GREY_DARK}[${(0, __intlayer_chokidar.formatPath)(baseFilePath)}${__intlayer_config.ANSIColors.GREY_DARK}][${(0, __intlayer_chokidar.formatLocale)(locale)}${__intlayer_config.ANSIColors.GREY_DARK}] `, { colSize: 40 }), `→ ${__intlayer_config.ANSIColors.RESET}`].join("");
	const { englishBlocks, frenchBlocks, plan, segmentsToReview } = require_translation_alignment_pipeline.buildAlignmentPlan({
		englishText,
		frenchText,
		changedLines
	});
	applicationLogger(`${filePrefix}Block-aware alignment complete. Total blocks: EN=${(0, __intlayer_config.colorizeNumber)(englishBlocks.length)}, FR=${(0, __intlayer_config.colorizeNumber)(frenchBlocks.length)}`);
	applicationLogger(`${filePrefix}Actions: reuse=${(0, __intlayer_config.colorizeNumber)(plan.actions.filter((a) => a.kind === "reuse").length)}, review=${(0, __intlayer_config.colorizeNumber)(plan.actions.filter((a) => a.kind === "review").length)}, new=${(0, __intlayer_config.colorizeNumber)(plan.actions.filter((a) => a.kind === "insert_new").length)}, delete=${(0, __intlayer_config.colorizeNumber)(plan.actions.filter((a) => a.kind === "delete").length)}`);
	if (segmentsToReview.length === 0) {
		applicationLogger(`${filePrefix}No segments need review, reusing existing translation`);
		(0, node_fs.mkdirSync)((0, node_path.dirname)(outputFilePath), { recursive: true });
		(0, node_fs.writeFileSync)(outputFilePath, require_translation_alignment_rebuildDocument.mergeReviewedSegments(plan, frenchBlocks, /* @__PURE__ */ new Map()));
		applicationLogger(`${(0, __intlayer_config.colorize)("✔", __intlayer_config.ANSIColors.GREEN)} File ${(0, __intlayer_chokidar.formatPath)(outputFilePath)} updated successfully (no changes needed).`);
		return;
	}
	applicationLogger(`${filePrefix}Segments to review: ${(0, __intlayer_config.colorizeNumber)(segmentsToReview.length)}`);
	const reviewedSegmentsMap = /* @__PURE__ */ new Map();
	for (const segment of segmentsToReview) {
		const segmentNumber = segmentsToReview.indexOf(segment) + 1;
		const englishBlock = segment.englishBlock;
		const getBaseChunkContextPrompt = () => `**BLOCK ${segmentNumber} of ${segmentsToReview.length}** is the base block in ${(0, __intlayer_chokidar.formatLocale)(baseLocale, false)} as reference.\n///chunksStart///\n` + englishBlock.content + `///chunksEnd///`;
		const getFrenchChunkPrompt = () => `**BLOCK ${segmentNumber} of ${segmentsToReview.length}** is the current block to review in ${(0, __intlayer_chokidar.formatLocale)(locale, false)}.\n///chunksStart///\n` + (segment.frenchBlockText ?? "") + `///chunksEnd///`;
		const reviewedChunkResult = await (0, __intlayer_config.retryManager)(async () => {
			const result = await require_utils_chunkInference.chunkInference([
				{
					role: "system",
					content: basePrompt
				},
				{
					role: "system",
					content: getBaseChunkContextPrompt()
				},
				{
					role: "system",
					content: getFrenchChunkPrompt()
				},
				{
					role: "system",
					content: `The next user message will be the **BLOCK ${(0, __intlayer_config.colorizeNumber)(segmentNumber)} of ${(0, __intlayer_config.colorizeNumber)(segmentsToReview.length)}** that should be translated in ${(0, __intlayer_core.getLocaleName)(locale, __intlayer_types.Locales.ENGLISH)} (${locale}).`
				},
				{
					role: "user",
					content: englishBlock.content
				}
			], aiOptions, configuration);
			applicationLogger(`${prefix}${(0, __intlayer_config.colorizeNumber)(result.tokenUsed)} tokens used - Block ${(0, __intlayer_config.colorizeNumber)(segmentNumber)} of ${(0, __intlayer_config.colorizeNumber)(segmentsToReview.length)}`);
			return require_utils_fixChunkStartEndChars.fixChunkStartEndChars(result?.fileContent, englishBlock.content);
		})();
		reviewedSegmentsMap.set(segment.actionIndex, reviewedChunkResult);
	}
	const finalFrenchOutput = require_translation_alignment_rebuildDocument.mergeReviewedSegments(plan, frenchBlocks, reviewedSegmentsMap);
	(0, node_fs.mkdirSync)((0, node_path.dirname)(outputFilePath), { recursive: true });
	(0, node_fs.writeFileSync)(outputFilePath, finalFrenchOutput);
	applicationLogger(`${(0, __intlayer_config.colorize)("✔", __intlayer_config.ANSIColors.GREEN)} File ${(0, __intlayer_chokidar.formatPath)(outputFilePath)} created/updated successfully.`);
};

//#endregion
exports.reviewFileBlockAware = reviewFileBlockAware;
//# sourceMappingURL=reviewDocBlockAware.cjs.map