import { formatLocale } from "@intlayer/chokidar";
import { ANSIColors, colon, colorize, colorizeKey, colorizePath, getAppLogger } from "@intlayer/config";
import { basename } from "node:path";
import { getUnmergedDictionaries } from "@intlayer/unmerged-dictionaries-entry";
import { getFilterTranslationsOnlyDictionary, getMissingLocalesContentFromDictionary } from "@intlayer/core";
import { getDictionaries } from "@intlayer/dictionaries-entry";

//#region src/fill/listTranslationsTasks.ts
const listTranslationsTasks = (localIds, outputLocales, mode, baseLocale, configuration) => {
	const appLogger = getAppLogger(configuration);
	const mergedDictionariesRecord = getDictionaries(configuration);
	const unmergedDictionariesRecord = getUnmergedDictionaries(configuration);
	const dictionariesToProcess = Object.values(unmergedDictionariesRecord).flat().filter((dictionary) => localIds.includes(dictionary.localId));
	const maxKeyLength = Math.max(...dictionariesToProcess.map((dict) => dict.key.length));
	const translationTasks = [];
	for (const targetUnmergedDictionary of dictionariesToProcess) {
		const dictionaryPreset = colon([
			" - ",
			colorize("[", ANSIColors.GREY_DARK),
			colorizeKey(targetUnmergedDictionary.key),
			colorize("]", ANSIColors.GREY_DARK)
		].join(""), { colSize: maxKeyLength + 6 });
		const dictionaryKey = targetUnmergedDictionary.key;
		const dictionaryLocalId = targetUnmergedDictionary.localId;
		const mainDictionaryToProcess = mergedDictionariesRecord[dictionaryKey];
		if ((targetUnmergedDictionary.fill ?? configuration.dictionary?.fill ?? false) === false) continue;
		const sourceLocale = targetUnmergedDictionary.locale ?? baseLocale;
		if (!mainDictionaryToProcess) {
			appLogger(`${dictionaryPreset} Dictionary not found in dictionariesRecord. Skipping.`, { level: "warn" });
			continue;
		}
		if (!targetUnmergedDictionary.filePath) {
			appLogger(`${dictionaryPreset} Dictionary has no file path. Skipping.`, { level: "warn" });
			continue;
		}
		const sourceLocaleContent = getFilterTranslationsOnlyDictionary(mainDictionaryToProcess, sourceLocale);
		if (Object.keys(sourceLocaleContent).length === 0) {
			appLogger(`${dictionaryPreset} No content found for dictionary in source locale ${formatLocale(sourceLocale)}. Skipping translation for this dictionary.`, { level: "warn" });
			continue;
		}
		/**
		* In 'complete' mode, filter only the missing locales to translate
		*
		* Skip the dictionary if there are no missing locales to translate
		*/
		let outputLocalesList = outputLocales;
		if (mode === "complete") {
			const isEmptyDictionary = typeof targetUnmergedDictionary.content === "object" && Object.keys(targetUnmergedDictionary.content).length === 0;
			if (Boolean(targetUnmergedDictionary.locale)) {
				const dictionaryLocale = targetUnmergedDictionary.locale;
				if (isEmptyDictionary) outputLocalesList = outputLocales.includes(dictionaryLocale) ? [dictionaryLocale] : [];
				else outputLocalesList = getMissingLocalesContentFromDictionary(targetUnmergedDictionary, outputLocales).includes(dictionaryLocale) ? [dictionaryLocale] : [];
			} else if (isEmptyDictionary) outputLocalesList = outputLocales;
			else outputLocalesList = getMissingLocalesContentFromDictionary(targetUnmergedDictionary, outputLocales);
		}
		if (outputLocalesList.length === 0) {
			appLogger(`${dictionaryPreset} No locales to fill, Skipping ${colorizePath(basename(targetUnmergedDictionary.filePath))}`, { level: "warn" });
			continue;
		}
		translationTasks.push({
			dictionaryKey,
			dictionaryLocalId,
			sourceLocale,
			targetLocales: outputLocalesList,
			dictionaryPreset,
			dictionaryFilePath: targetUnmergedDictionary.filePath
		});
	}
	return translationTasks;
};

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