import { parallelize } from "../utils/parallelize.mjs";
import { writeFileIfChanged } from "../writeFileIfChanged.mjs";
import { generateDictionaryListContent } from "./generateDictionaryListContent.mjs";
import { getBuiltDictionariesPath } from "./getBuiltDictionariesPath.mjs";
import { getBuiltDynamicDictionariesPath } from "./getBuiltDynamicDictionariesPath.mjs";
import { getBuiltFetchDictionariesPath } from "./getBuiltFetchDictionariesPath.mjs";
import { getBuiltRemoteDictionariesPath } from "./getBuiltRemoteDictionariesPath.mjs";
import { getBuiltUnmergedDictionariesPath } from "./getBuiltUnmergedDictionariesPath.mjs";
import { resolve } from "node:path";
import { mkdir } from "node:fs/promises";
import { getConfiguration } from "@intlayer/config";

//#region ../@intlayer/chokidar/dist/esm/createDictionaryEntryPoint/createDictionaryEntryPoint.mjs
const writeDictionaryFiles = async (paths, fileName, functionName, format, configuration = getConfiguration()) => {
	const content = generateDictionaryListContent(await paths, functionName, format, configuration);
	const extension = format === "cjs" ? "cjs" : "mjs";
	const { mainDir } = configuration.content;
	await writeFileIfChanged(resolve(mainDir, `${fileName}.${extension}`), content);
};
/**
* This function generates a list of dictionaries in the main directory
*/
const createDictionaryEntryPoint = async (configuration = getConfiguration(), formats) => {
	const outputFormats = formats ?? configuration.build.outputFormat;
	const { mainDir } = configuration.content;
	await mkdir(mainDir, { recursive: true });
	await parallelize([
		...outputFormats.map((format) => ({
			paths: getBuiltRemoteDictionariesPath(configuration),
			functionName: "getRemoteDictionaries",
			fileName: "remote_dictionaries",
			format
		})),
		...outputFormats.map((format) => ({
			paths: getBuiltDictionariesPath(configuration),
			functionName: "getDictionaries",
			fileName: "dictionaries",
			format
		})),
		...outputFormats.map((format) => ({
			paths: getBuiltUnmergedDictionariesPath(configuration),
			functionName: "getUnmergedDictionaries",
			fileName: "unmerged_dictionaries",
			format
		})),
		...outputFormats.map((format) => ({
			paths: getBuiltDynamicDictionariesPath(configuration, format),
			functionName: "getDynamicDictionaries",
			fileName: "dynamic_dictionaries",
			format
		})),
		...outputFormats.map((format) => ({
			paths: getBuiltFetchDictionariesPath(configuration, format),
			functionName: "getFetchDictionaries",
			fileName: "fetch_dictionaries",
			format
		}))
	], async ({ paths, fileName, format, functionName }) => writeDictionaryFiles(paths, fileName, functionName, format, configuration));
};

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