const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.cjs');
const require_logger = require('../../../config/dist/esm/logger.cjs');
const require_retryManager = require('../../../config/dist/esm/retryManager.cjs');
const require_getConfiguration = require('../../../config/dist/esm/configFile/getConfiguration.cjs');
const require_formatter = require('./chokidar/dist/esm/utils/formatter.cjs');
const require_getLocaleName = require('./core/dist/esm/localization/getLocaleName.cjs');
const require__utils_asset = require('./_virtual/_utils_asset.cjs');
const require_locales = require('./types/dist/esm/locales.cjs');
const require_rebuildDocument = require('./translation-alignment/rebuildDocument.cjs');
const require_pipeline = require('./translation-alignment/pipeline.cjs');
const require_chunkInference = require('./utils/chunkInference.cjs');
const require_fixChunkStartEndChars = require('./utils/fixChunkStartEndChars.cjs');
let node_path = require("node:path");
let node_fs_promises = require("node:fs/promises");
let node_fs = require("node:fs");

//#region ../@intlayer/cli/dist/esm/reviewDocBlockAware.mjs
/**
* 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 = require_getConfiguration.getConfiguration(configOptions);
	const applicationLogger = require_logger.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}}", `${require_formatter.formatLocale(locale, false)}`).replaceAll("{{baseLocaleName}}", `${require_formatter.formatLocale(baseLocale, false)}`).replace("{{applicationContext}}", aiOptions?.applicationContext ?? "-").replace("{{customInstructions}}", customInstructions ?? "-");
	const filePrefix = [require_logger.colon(`${require_logger.ANSIColors.GREY_DARK}[${require_formatter.formatPath(baseFilePath)}${require_logger.ANSIColors.GREY_DARK}] `, { colSize: 40 }), `→ ${require_logger.ANSIColors.RESET}`].join("");
	const prefix = [require_logger.colon(`${require_logger.ANSIColors.GREY_DARK}[${require_formatter.formatPath(baseFilePath)}${require_logger.ANSIColors.GREY_DARK}][${require_formatter.formatLocale(locale)}${require_logger.ANSIColors.GREY_DARK}] `, { colSize: 40 }), `→ ${require_logger.ANSIColors.RESET}`].join("");
	const { englishBlocks, frenchBlocks, plan, segmentsToReview } = require_pipeline.buildAlignmentPlan({
		englishText,
		frenchText,
		changedLines
	});
	applicationLogger(`${filePrefix}Block-aware alignment complete. Total blocks: EN=${require_logger.colorizeNumber(englishBlocks.length)}, FR=${require_logger.colorizeNumber(frenchBlocks.length)}`);
	applicationLogger(`${filePrefix}Actions: reuse=${require_logger.colorizeNumber(plan.actions.filter((a) => a.kind === "reuse").length)}, review=${require_logger.colorizeNumber(plan.actions.filter((a) => a.kind === "review").length)}, new=${require_logger.colorizeNumber(plan.actions.filter((a) => a.kind === "insert_new").length)}, delete=${require_logger.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_rebuildDocument.mergeReviewedSegments(plan, frenchBlocks, /* @__PURE__ */ new Map()));
		applicationLogger(`${require_logger.colorize("✔", require_logger.ANSIColors.GREEN)} File ${require_formatter.formatPath(outputFilePath)} updated successfully (no changes needed).`);
		return;
	}
	applicationLogger(`${filePrefix}Segments to review: ${require_logger.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 ${require_formatter.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 ${require_formatter.formatLocale(locale, false)}.\n///chunksStart///\n` + (segment.frenchBlockText ?? "") + `///chunksEnd///`;
		const reviewedChunkResult = await require_retryManager.retryManager(async () => {
			const result = await require_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 ${require_logger.colorizeNumber(segmentNumber)} of ${require_logger.colorizeNumber(segmentsToReview.length)}** that should be translated in ${require_getLocaleName.getLocaleName(locale, require_locales.locales_exports.ENGLISH)} (${locale}).`
				},
				{
					role: "user",
					content: englishBlock.content
				}
			], aiOptions, configuration);
			applicationLogger(`${prefix}${require_logger.colorizeNumber(result.tokenUsed)} tokens used - Block ${require_logger.colorizeNumber(segmentNumber)} of ${require_logger.colorizeNumber(segmentsToReview.length)}`);
			return require_fixChunkStartEndChars.fixChunkStartEndChars(result?.fileContent, englishBlock.content);
		})();
		reviewedSegmentsMap.set(segment.actionIndex, reviewedChunkResult);
	}
	const finalFrenchOutput = require_rebuildDocument.mergeReviewedSegments(plan, frenchBlocks, reviewedSegmentsMap);
	(0, node_fs.mkdirSync)((0, node_path.dirname)(outputFilePath), { recursive: true });
	(0, node_fs.writeFileSync)(outputFilePath, finalFrenchOutput);
	applicationLogger(`${require_logger.colorize("✔", require_logger.ANSIColors.GREEN)} File ${require_formatter.formatPath(outputFilePath)} created/updated successfully.`);
};

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