const require_rolldown_runtime = require('../../../../../../../../_virtual/rolldown_runtime.cjs');
const require_logger = require('../../../../../../../config/dist/esm/logger.cjs');
const require_parallelize = require('../utils/parallelize.cjs');
const require_writeJsonIfChanged = require('../writeJsonIfChanged.cjs');
const require_orderDictionaries = require('../core/dist/esm/dictionaryManipulator/orderDictionaries.cjs');
const require_filterInvalidDictionaries = require('../filterInvalidDictionaries.cjs');
let node_path = require("node:path");
let node_fs_promises = require("node:fs/promises");

//#region ../@intlayer/webpack/dist/esm/chokidar/dist/esm/buildIntlayerDictionary/writeUnmergedDictionary.mjs
const groupDictionariesByKey = (dictionaries) => dictionaries.reduce((acc, dictionary) => {
	const key = dictionary.key;
	if (!acc[key]) acc[key] = [];
	acc[key].push(dictionary);
	return acc;
}, {});
/**
* Write the unmerged dictionaries to the unmergedDictionariesDir
* @param dictionaries - The dictionaries to write
* @param configuration - The configuration
* @returns The grouped dictionaries
*
* @example
* ```ts
* const unmergedDictionaries = await writeUnmergedDictionaries(dictionaries);
* console.log(unmergedDictionaries);
*
* // .intlayer/unmerged_dictionaries/home.json
* // {
* //   [
* //     { key: 'home', content: { ... } },
* //     { key: 'home', content: { ... } },
* //   ],
* // }
* ```
*/
const writeUnmergedDictionaries = async (dictionaries, configuration) => {
	const { unmergedDictionariesDir } = configuration.content;
	await (0, node_fs_promises.mkdir)((0, node_path.resolve)(unmergedDictionariesDir), { recursive: true });
	const groupedDictionaries = groupDictionariesByKey(require_filterInvalidDictionaries.filterInvalidDictionaries(dictionaries, configuration));
	const results = await require_parallelize.parallelize(Object.entries(groupedDictionaries), async ([key, dictionaries$1]) => {
		if (key === "undefined") return;
		const orderedDictionaries = require_orderDictionaries.orderDictionaries(dictionaries$1, configuration);
		const unmergedFilePath = (0, node_path.resolve)(unmergedDictionariesDir, `${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: dictionaries$1
		}];
	});
	return Object.fromEntries(results.filter(Boolean));
};

//#endregion
exports.groupDictionariesByKey = groupDictionariesByKey;
exports.writeUnmergedDictionaries = writeUnmergedDictionaries;
//# sourceMappingURL=writeUnmergedDictionary.cjs.map