import { cleanOutputDir } from "./cleanOutputDir.mjs";
import { buildDictionary } from "./buildIntlayerDictionary/buildIntlayerDictionary.mjs";
import { createDictionaryEntryPoint } from "./createDictionaryEntryPoint/createDictionaryEntryPoint.mjs";
import { createModuleAugmentation } from "./createType/createModuleAugmentation.mjs";
import { createTypes } from "./createType/createType.mjs";
import { listDictionaries } from "./listDictionariesPath.mjs";
import { loadDictionaries } from "./loadDictionaries/loadDictionaries.mjs";
import { runOnce } from "./utils/runOnce.mjs";
import { version } from "./config/package.mjs";
import { writeRemoteDictionary } from "./buildIntlayerDictionary/writeRemoteDictionary.mjs";
import { writeConfiguration } from "./writeConfiguration/index.mjs";
import { join } from "node:path";
import { ANSIColors, colorize, getAppLogger } from "@intlayer/config";

//#region ../chokidar/dist/esm/prepareIntlayer.mjs
const DEFAULT_PREPARE_INTLAYER_OPTIONS = {
	clean: false,
	format: ["cjs", "esm"],
	forceRun: false
};
const prepareIntlayer = async (configuration, options) => {
	const { clean, format, forceRun, onIsCached } = {
		...DEFAULT_PREPARE_INTLAYER_OPTIONS,
		...options ?? {}
	};
	const appLogger = getAppLogger(configuration);
	await runOnce(join(configuration.content.cacheDir, "intlayer-prepared.lock"), async () => {
		const { plugins } = configuration;
		const preparationStartMs = Date.now();
		appLogger(["Preparing Intlayer", colorize(`(v${version})`, ANSIColors.GREY_DARK)]);
		if (clean) cleanOutputDir(configuration);
		await writeConfiguration(configuration);
		const configurationWrittenTime = Date.now();
		appLogger(["Configuration written", colorize(`(${configurationWrittenTime - preparationStartMs}ms)`, ANSIColors.GREY_DARK)], { isVerbose: true });
		const dictionaries = await loadDictionaries(await listDictionaries(configuration), configuration);
		const dictionariesLoadedTime = Date.now();
		appLogger(["Content loaded", colorize([dictionaries.remoteDictionaries.length + dictionaries.pluginDictionaries.length > 0 ? [
			`(Total: ${dictionariesLoadedTime - configurationWrittenTime}ms`,
			dictionaries.localDictionaries.length > 0 ? `- Local: ${dictionaries.time.localDictionaries}ms` : "",
			dictionaries.remoteDictionaries.length > 0 ? `- Remote: ${dictionaries.time.remoteDictionaries}ms` : "",
			dictionaries.pluginDictionaries.length > 0 ? `- Plugin: ${dictionaries.time.pluginDictionaries}ms` : "",
			`)`
		].join("") : `(${dictionariesLoadedTime - configurationWrittenTime}ms)`].join(""), ANSIColors.GREY_DARK)], { isVerbose: true });
		const dictionariesOutput = await buildDictionary([
			...dictionaries.localDictionaries,
			...dictionaries.remoteDictionaries,
			...dictionaries.pluginDictionaries
		], configuration, format, false);
		await writeRemoteDictionary(dictionaries.remoteDictionaries, configuration);
		await createTypes(Object.values(dictionariesOutput?.mergedDictionaries ?? {}).map((dictionary) => dictionary.dictionaryPath), configuration);
		await createDictionaryEntryPoint(configuration);
		const dictionariesBuiltTime = Date.now();
		appLogger(["Dictionaries built", colorize(`(${dictionariesBuiltTime - preparationStartMs}ms)`, ANSIColors.GREY_DARK)]);
		await createModuleAugmentation(configuration);
		appLogger(["Module augmentation built", colorize(`(${Date.now() - dictionariesBuiltTime}ms)`, ANSIColors.GREY_DARK)], { isVerbose: true });
		for await (const plugin of plugins ?? []) {
			const { unmergedDictionaries, mergedDictionaries } = dictionariesOutput;
			await plugin.afterBuild?.({
				dictionaries: {
					unmergedDictionaries,
					mergedDictionaries
				},
				configuration
			});
		}
		appLogger([`Done`, colorize(`${Date.now() - preparationStartMs}ms`, ANSIColors.GREEN)], {
			level: "info",
			isVerbose: true
		});
	}, {
		forceRun,
		onIsCached,
		cacheTimeoutMs: 1e3 * 60 * 60
	});
};

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