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_parallelize = require('../utils/parallelize.cjs');
const require_writeJsonIfChanged = require('../writeJsonIfChanged.cjs');
const require_mergeDictionaries = require('../core/dist/esm/dictionaryManipulator/mergeDictionaries.cjs');
const require_normalizeDictionary = require('../core/dist/esm/dictionaryManipulator/normalizeDictionary.cjs');
let node_path = require("node:path");
let node_fs_promises = require("node:fs/promises");

//#region ../chokidar/dist/esm/buildIntlayerDictionary/writeMergedDictionary.mjs
/**
* Write the merged dictionaries to the dictionariesDir
* @param groupedDictionaries - The grouped dictionaries
* @param configuration - The configuration
* @returns The merged dictionaries
*
* @example
* ```ts
* const unmergedDictionaries = await writeUnmergedDictionaries(dictionaries);
* const finalDictionaries = await writeFinalDictionaries(unmergedDictionaries);
* console.log(finalDictionaries);
*
* // .intlayer/dictionary/home.json
* // { key: 'home', content: { ... } },
* ```
*/
const writeMergedDictionaries = async (groupedDictionaries, configuration = require_getConfiguration.getConfiguration()) => {
	const { dictionariesDir } = configuration.content;
	await (0, node_fs_promises.mkdir)((0, node_path.resolve)(dictionariesDir), { recursive: true });
	const results = await require_parallelize.parallelize(Object.entries(groupedDictionaries), async ([key, dictionariesEntry]) => {
		if (key === "undefined") return;
		const mergedDictionary = require_mergeDictionaries.mergeDictionaries(require_normalizeDictionary.normalizeDictionaries(dictionariesEntry.dictionaries, configuration));
		const resultFilePath = (0, node_path.resolve)(dictionariesDir, `${key}.json`);
		await require_writeJsonIfChanged.writeJsonIfChanged(resultFilePath, mergedDictionary).catch((err) => {
			console.error(`Error creating merged ${require_logger.colorizePath(resultFilePath)}:`, err);
		});
		return [key, {
			dictionaryPath: resultFilePath,
			dictionary: mergedDictionary
		}];
	});
	return Object.fromEntries(results.filter(Boolean));
};

//#endregion
exports.writeMergedDictionaries = writeMergedDictionaries;
//# sourceMappingURL=writeMergedDictionary.cjs.map