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_filterInvalidDictionaries = require('../filterInvalidDictionaries.cjs');
const require_writeJsonIfChanged = require('../writeJsonIfChanged.cjs');
const require_orderDictionaries = require('../core/dist/esm/dictionaryManipulator/orderDictionaries.cjs');
const require_writeUnmergedDictionary = require('./writeUnmergedDictionary.cjs');
const require_loadRemoteDictionaries = require('../loadDictionaries/loadRemoteDictionaries.cjs');
let node_path = require("node:path");
let node_fs_promises = require("node:fs/promises");

//#region ../@intlayer/chokidar/dist/esm/buildIntlayerDictionary/writeRemoteDictionary.mjs
/**
* Write the localized dictionaries to the dictionariesDir
* @param mergedDictionaries - The merged dictionaries
* @param configuration - The configuration
* @returns The final dictionaries
*
* @example
* ```ts
* const unmergedDictionaries = await writeUnmergedDictionaries(dictionaries);
* const finalDictionaries = await writeFinalDictionaries(unmergedDictionaries);
* console.log(finalDictionaries);
*
* // .intlayer/fetch_dictionaries/home.json
* // { key: 'home', content: { ... } },
* ```
*/
const writeRemoteDictionary = async (remoteDictionaries, configuration = require_getConfiguration.getConfiguration()) => {
	const { remoteDictionariesDir } = configuration.content;
	await (0, node_fs_promises.mkdir)((0, node_path.resolve)(remoteDictionariesDir), { recursive: true });
	const groupedDictionaries = require_writeUnmergedDictionary.groupDictionariesByKey(require_filterInvalidDictionaries.filterInvalidDictionaries(remoteDictionaries, configuration));
	const results = await require_parallelize.parallelize(Object.entries(groupedDictionaries), async ([key, dictionaries]) => {
		if (key === "undefined") return;
		const orderedDictionaries = require_orderDictionaries.orderDictionaries(require_loadRemoteDictionaries.formatDistantDictionaries(dictionaries), configuration);
		const unmergedFilePath = (0, node_path.resolve)(remoteDictionariesDir, `${key}.json`);
		await require_writeJsonIfChanged.writeJsonIfChanged(unmergedFilePath, orderedDictionaries).catch((err) => {
			console.error(`${require_logger.x} Error creating unmerged ${require_logger.colorizePath(unmergedFilePath)}:`, err);
		});
		return [key, {
			dictionaryPath: unmergedFilePath,
			dictionaries
		}];
	});
	return Object.fromEntries(results.filter(Boolean));
};

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