import { formatLocale, formatPath } from "./chokidar/dist/esm/utils/formatter.mjs";
import { parallelize } from "./chokidar/dist/esm/utils/parallelize.mjs";
import { listGitFiles, listGitLines } from "./chokidar/dist/esm/listGitFiles.mjs";
import { checkAIAccess } from "./utils/checkAccess.mjs";
import { reviewFileBlockAware } from "./reviewDocBlockAware.mjs";
import { checkFileModifiedRange } from "./utils/checkFileModifiedRange.mjs";
import { getOutputFilePath } from "./utils/getOutputFilePath.mjs";
import { join, relative } from "node:path";
import { existsSync } from "node:fs";
import { ANSIColors, colorize, colorizeNumber, getAppLogger, getConfiguration } from "@intlayer/config";
import fg from "fast-glob";

//#region ../@intlayer/cli/dist/esm/reviewDoc.mjs
/**
* Main audit function: scans all .md files in "en/" (unless you specified DOC_LIST),
* then audits them to each locale in LOCALE_LIST.
*/
const reviewDoc = async ({ docPattern, locales, excludedGlobPattern, baseLocale, aiOptions, nbSimultaneousFileProcessed, configOptions, customInstructions, skipIfModifiedBefore, skipIfModifiedAfter, skipIfExists, gitOptions }) => {
	const configuration = getConfiguration(configOptions);
	const appLogger = getAppLogger(configuration);
	if (!await checkAIAccess(configuration, aiOptions)) return;
	if (nbSimultaneousFileProcessed && nbSimultaneousFileProcessed > 10) {
		appLogger(`Warning: nbSimultaneousFileProcessed is set to ${nbSimultaneousFileProcessed}, which is greater than 10. Setting it to 10.`);
		nbSimultaneousFileProcessed = 10;
	}
	let docList = await fg(docPattern, { ignore: excludedGlobPattern });
	if (gitOptions) {
		const gitChangedFiles = await listGitFiles(gitOptions);
		if (gitChangedFiles) docList = docList.filter((path) => gitChangedFiles.some((gitFile) => join(process.cwd(), path) === gitFile));
	}
	appLogger(`Base locale is ${formatLocale(baseLocale)}`);
	appLogger(`Reviewing ${colorizeNumber(locales.length)} locales: [ ${formatLocale(locales)} ]`);
	appLogger(`Reviewing ${colorizeNumber(docList.length)} files:`);
	appLogger(docList.map((path) => ` - ${formatPath(path)}\n`));
	await parallelize(docList.flatMap((docPath) => locales.map((locale) => async () => {
		appLogger(`Reviewing file: ${formatPath(docPath)} to ${formatLocale(locale)}`);
		const absoluteBaseFilePath = join(configuration.content.baseDir, docPath);
		const outputFilePath = getOutputFilePath(absoluteBaseFilePath, locale, baseLocale);
		if (skipIfExists && existsSync(outputFilePath)) {
			const relativePath = relative(configuration.content.baseDir, outputFilePath);
			appLogger(`${colorize("⊘", ANSIColors.YELLOW)} File ${formatPath(relativePath)} already exists, skipping.`);
			return;
		}
		const fileModificationData = checkFileModifiedRange(outputFilePath, {
			skipIfModifiedBefore,
			skipIfModifiedAfter
		});
		if (fileModificationData.isSkipped) {
			appLogger(fileModificationData.message);
			return;
		}
		let changedLines;
		if (gitOptions) {
			const gitChangedLines = await listGitLines(absoluteBaseFilePath, gitOptions);
			appLogger(`Git changed lines: ${gitChangedLines.join(", ")}`);
			changedLines = gitChangedLines;
		}
		await reviewFileBlockAware(absoluteBaseFilePath, outputFilePath, locale, baseLocale, aiOptions, configOptions, customInstructions, changedLines);
	})), (task) => task(), nbSimultaneousFileProcessed ?? 3);
};

//#endregion
export { reviewDoc };
//# sourceMappingURL=reviewDoc.mjs.map