const require_rolldown_runtime = require('../../../../../_virtual/rolldown_runtime.cjs');
const require_logger = require('../../../../config/dist/esm/logger.cjs');
const require_getConfiguration = require('../../../../config/dist/esm/configFile/getConfiguration.cjs');
const require_formatter = require('../chokidar/dist/esm/utils/formatter.cjs');
const require_prepareIntlayer = require('../chokidar/dist/esm/prepareIntlayer.cjs');
const require_parallelizeGlobal = require('../chokidar/dist/esm/utils/parallelizeGlobal.cjs');
const require_writeContentDeclaration = require('../chokidar/dist/esm/writeContentDeclaration/writeContentDeclaration.cjs');
const require_getTargetDictionary = require('../getTargetDictionary.cjs');
const require_checkAccess = require('../utils/checkAccess.cjs');
const require_listTranslationsTasks = require('./listTranslationsTasks.cjs');
const require_translateDictionary = require('./translateDictionary.cjs');
const require_writeFill = require('./writeFill.cjs');
let node_path = require("node:path");

//#region ../@intlayer/cli/dist/esm/fill/fill.mjs
const NB_CONCURRENT_TRANSLATIONS = 7;
/**
* Fill translations based on the provided options.
*/
const fill = async (options) => {
	const configuration = require_getConfiguration.getConfiguration(options?.configOptions);
	const appLogger = require_logger.getAppLogger(configuration);
	if (options?.build === true) await require_prepareIntlayer.prepareIntlayer(configuration, { forceRun: true });
	else if (typeof options?.build === "undefined") await require_prepareIntlayer.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_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) => require_logger.colorizeKey(key)).join(", ") : require_logger.colorize("No keys found", require_logger.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_listTranslationsTasks.listTranslationsTasks(targetUnmergedDictionaries.map((dictionary) => dictionary.localId), outputLocales, mode, baseLocale, configuration);
	const nbConcurrentTranslations = options?.nbConcurrentTranslations ?? NB_CONCURRENT_TRANSLATIONS;
	const globalLimiter = require_parallelizeGlobal.getGlobalLimiter(nbConcurrentTranslations);
	const taskLimiter = require_parallelizeGlobal.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 ${require_logger.colorizePath((0, node_path.basename)(relativePath))}`, { level: "info" });
		const translationTaskResult = await require_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_writeFill.writeFill(dictionaryOutput, outputLocales, [sourceLocale], configuration);
		else {
			await require_writeContentDeclaration.writeContentDeclaration(dictionaryOutput, configuration);
			if (dictionaryOutput.filePath) appLogger(`${require_logger.colon([
				" - ",
				require_logger.colorize("[", require_logger.ANSIColors.GREY_DARK),
				require_logger.colorizeKey(dictionaryOutput.key),
				require_logger.colorize("]", require_logger.ANSIColors.GREY_DARK)
			].join(""), { colSize: 15 })} Content declaration written to ${require_formatter.formatPath((0, node_path.basename)(dictionaryOutput.filePath))}`, { level: "info" });
		}
	}));
	await Promise.all(runners);
	await globalLimiter.onIdle();
};

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