const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
const require_getTargetDictionary = require('../getTargetDictionary.cjs');
const require_utils_checkAccess = require('../utils/checkAccess.cjs');
const require_fill_listTranslationsTasks = require('./listTranslationsTasks.cjs');
const require_fill_translateDictionary = require('./translateDictionary.cjs');
const require_fill_writeFill = require('./writeFill.cjs');
let __intlayer_chokidar = require("@intlayer/chokidar");
let __intlayer_config = require("@intlayer/config");
let node_path = require("node:path");

//#region src/fill/fill.ts
const NB_CONCURRENT_TRANSLATIONS = 7;
/**
* Fill translations based on the provided options.
*/
const fill = async (options) => {
	const configuration = (0, __intlayer_config.getConfiguration)(options?.configOptions);
	const appLogger = (0, __intlayer_config.getAppLogger)(configuration);
	if (options?.build === true) await (0, __intlayer_chokidar.prepareIntlayer)(configuration, { forceRun: true });
	else if (typeof options?.build === "undefined") await (0, __intlayer_chokidar.prepareIntlayer)(configuration);
	const { defaultLocale, locales } = configuration.internationalization;
	const mode = options?.mode ?? "complete";
	const baseLocale = options?.sourceLocale ?? defaultLocale;
	const outputLocales = options?.outputLocales ? require_getTargetDictionary.ensureArray(options.outputLocales) : locales;
	if (!await require_utils_checkAccess.checkAIAccess(configuration, options?.aiOptions)) return;
	const targetUnmergedDictionaries = await require_getTargetDictionary.getTargetUnmergedDictionaries(options);
	const affectedDictionaryKeys = /* @__PURE__ */ new Set();
	targetUnmergedDictionaries.forEach((dict) => {
		affectedDictionaryKeys.add(dict.key);
	});
	const keysToProcess = Array.from(affectedDictionaryKeys);
	appLogger(["Affected dictionary keys for processing:", keysToProcess.length > 0 ? keysToProcess.map((key) => (0, __intlayer_config.colorizeKey)(key)).join(", ") : (0, __intlayer_config.colorize)("No keys found", __intlayer_config.ANSIColors.YELLOW)]);
	if (keysToProcess.length === 0) return;
	/**
	* List the translations tasks
	*
	* Create a list of per-locale dictionaries to translate
	*
	* In 'complete' mode, filter only the missing locales to translate
	*/
	const translationTasks = require_fill_listTranslationsTasks.listTranslationsTasks(targetUnmergedDictionaries.map((dictionary) => dictionary.localId), outputLocales, mode, baseLocale, configuration);
	const nbConcurrentTranslations = options?.nbConcurrentTranslations ?? NB_CONCURRENT_TRANSLATIONS;
	const globalLimiter = (0, __intlayer_chokidar.getGlobalLimiter)(nbConcurrentTranslations);
	const taskLimiter = (0, __intlayer_chokidar.getTaskLimiter)(Math.max(1, Math.min(options?.nbConcurrentTasks ?? nbConcurrentTranslations, translationTasks.length)));
	const runners = translationTasks.map((task) => taskLimiter(async () => {
		const relativePath = (0, node_path.relative)(configuration?.content?.baseDir ?? process.cwd(), task?.dictionaryFilePath ?? "");
		appLogger(`${task.dictionaryPreset} Processing ${(0, __intlayer_config.colorizePath)((0, node_path.basename)(relativePath))}`, { level: "info" });
		const translationTaskResult = await require_fill_translateDictionary.translateDictionary(task, configuration, {
			mode,
			aiOptions: options?.aiOptions,
			fillMetadata: !options?.skipMetadata,
			onHandle: globalLimiter
		});
		if (!translationTaskResult?.dictionaryOutput) return;
		const { dictionaryOutput, sourceLocale } = translationTaskResult;
		if (typeof dictionaryOutput.fill === "string" || typeof dictionaryOutput.fill === "object") await require_fill_writeFill.writeFill(dictionaryOutput, outputLocales, [sourceLocale], configuration);
		else {
			await (0, __intlayer_chokidar.writeContentDeclaration)(dictionaryOutput, configuration);
			if (dictionaryOutput.filePath) appLogger(`${(0, __intlayer_config.colon)([
				" - ",
				(0, __intlayer_config.colorize)("[", __intlayer_config.ANSIColors.GREY_DARK),
				(0, __intlayer_config.colorizeKey)(dictionaryOutput.key),
				(0, __intlayer_config.colorize)("]", __intlayer_config.ANSIColors.GREY_DARK)
			].join(""), { colSize: 15 })} Content declaration written to ${(0, __intlayer_chokidar.formatPath)((0, node_path.basename)(dictionaryOutput.filePath))}`, { level: "info" });
		}
	}));
	await Promise.all(runners);
	await globalLimiter.onIdle();
};

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