import { getUnmergedDictionaries } from "../unmerged-dictionaries-entry/dist/esm/index.mjs";
import { writeDynamicDictionary } from "./writeDynamicDictionary.mjs";
import { writeFetchDictionary } from "./writeFetchDictionary.mjs";
import { writeMergedDictionaries } from "./writeMergedDictionary.mjs";
import { writeUnmergedDictionaries } from "./writeUnmergedDictionary.mjs";
import { getConfiguration } from "@intlayer/config";

//#region ../@intlayer/cli/dist/esm/chokidar/dist/esm/buildIntlayerDictionary/buildIntlayerDictionary.mjs
/**
* This function transpile the bundled code to to make dictionaries as JSON files
*/
const buildDictionary = async (localDictionariesEntries, configuration = getConfiguration(), formats = ["cjs", "esm"], importOtherDictionaries = true) => {
	const { importMode } = configuration.build;
	const unmergedDictionariesToUpdate = [...localDictionariesEntries];
	if (importOtherDictionaries) {
		const prevUnmergedDictionaries = getUnmergedDictionaries(configuration);
		for (const dictionaryToWrite of localDictionariesEntries) {
			const allPrebuiltUnmergedDictionaries = prevUnmergedDictionaries[dictionaryToWrite.key];
			if (allPrebuiltUnmergedDictionaries?.length > 0) {
				const otherUnmergedDictionaries = allPrebuiltUnmergedDictionaries.filter((unmergedDictionary) => unmergedDictionary.localId !== dictionaryToWrite.localId);
				unmergedDictionariesToUpdate.push(...otherUnmergedDictionaries);
			}
		}
	}
	const unmergedDictionaries = await writeUnmergedDictionaries(unmergedDictionariesToUpdate, configuration);
	const mergedDictionaries = await writeMergedDictionaries(unmergedDictionaries, configuration);
	let dynamicDictionaries = null;
	if (importMode === "dynamic" || importMode === "live") dynamicDictionaries = await writeDynamicDictionary(mergedDictionaries, configuration, formats);
	let fetchDictionaries = null;
	if (importMode === "live") fetchDictionaries = await writeFetchDictionary(dynamicDictionaries, configuration, formats);
	return {
		unmergedDictionaries,
		mergedDictionaries,
		dynamicDictionaries,
		fetchDictionaries
	};
};

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