const require_rolldown_runtime = require('../../../../../../../_virtual/rolldown_runtime.cjs');
const require_logger = require('../../../../../../config/dist/esm/logger.cjs');
const require_buildIntlayerDictionary = require('./buildIntlayerDictionary/buildIntlayerDictionary.cjs');
const require_createDictionaryEntryPoint = require('./createDictionaryEntryPoint/createDictionaryEntryPoint.cjs');
const require_createModuleAugmentation = require('./createType/createModuleAugmentation.cjs');
const require_createType = require('./createType/createType.cjs');
const require_listDictionariesPath = require('./listDictionariesPath.cjs');
const require_cleanOutputDir = require('./cleanOutputDir.cjs');
const require_loadDictionaries = require('./loadDictionaries/loadDictionaries.cjs');
const require_runOnce = require('./utils/runOnce.cjs');
const require_package = require('./config/package.cjs');
const require_writeRemoteDictionary = require('./buildIntlayerDictionary/writeRemoteDictionary.cjs');
const require_index = require('./writeConfiguration/index.cjs');
let node_path = require("node:path");

//#region ../@intlayer/cli/dist/esm/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 = require_logger.getAppLogger(configuration);
	await require_runOnce.runOnce((0, node_path.join)(configuration.content.cacheDir, "intlayer-prepared.lock"), async () => {
		const { plugins } = configuration;
		const preparationStartMs = Date.now();
		appLogger(["Preparing Intlayer", require_logger.colorize(`(v${require_package.version})`, require_logger.ANSIColors.GREY_DARK)]);
		if (clean) require_cleanOutputDir.cleanOutputDir(configuration);
		await require_index.writeConfiguration(configuration);
		const configurationWrittenTime = Date.now();
		appLogger(["Configuration written", require_logger.colorize(`(${configurationWrittenTime - preparationStartMs}ms)`, require_logger.ANSIColors.GREY_DARK)], { isVerbose: true });
		const dictionaries = await require_loadDictionaries.loadDictionaries(await require_listDictionariesPath.listDictionaries(configuration), configuration);
		const dictionariesLoadedTime = Date.now();
		appLogger(["Content loaded", require_logger.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(""), require_logger.ANSIColors.GREY_DARK)], { isVerbose: true });
		const dictionariesOutput = await require_buildIntlayerDictionary.buildDictionary([
			...dictionaries.localDictionaries,
			...dictionaries.remoteDictionaries,
			...dictionaries.pluginDictionaries
		], configuration, format, false);
		await require_writeRemoteDictionary.writeRemoteDictionary(dictionaries.remoteDictionaries, configuration);
		await require_createType.createTypes(Object.values(dictionariesOutput?.mergedDictionaries ?? {}).map((dictionary) => dictionary.dictionaryPath), configuration);
		await require_createDictionaryEntryPoint.createDictionaryEntryPoint(configuration);
		const dictionariesBuiltTime = Date.now();
		appLogger(["Dictionaries built", require_logger.colorize(`(${dictionariesBuiltTime - preparationStartMs}ms)`, require_logger.ANSIColors.GREY_DARK)]);
		await require_createModuleAugmentation.createModuleAugmentation(configuration);
		appLogger(["Module augmentation built", require_logger.colorize(`(${Date.now() - dictionariesBuiltTime}ms)`, require_logger.ANSIColors.GREY_DARK)], { isVerbose: true });
		for await (const plugin of plugins ?? []) {
			const { unmergedDictionaries, mergedDictionaries } = dictionariesOutput;
			await plugin.afterBuild?.({
				dictionaries: {
					unmergedDictionaries,
					mergedDictionaries
				},
				configuration
			});
		}
		appLogger([`Done`, require_logger.colorize(`${Date.now() - preparationStartMs}ms`, require_logger.ANSIColors.GREEN)], {
			level: "info",
			isVerbose: true
		});
	}, {
		forceRun,
		onIsCached,
		cacheTimeoutMs: 1e3 * 60 * 60
	});
};

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