const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.cjs');
const require_cache = require('../../../../config/dist/esm/utils/cache.cjs');
const require_ESMxCJSHelpers = require('../../../../config/dist/esm/utils/ESMxCJSHelpers.cjs');
const require_loadExternalFile = require('../../../../config/dist/esm/loadExternalFile/loadExternalFile.cjs');
const require_parallelize = require('../utils/parallelize.cjs');
const require_filterInvalidDictionaries = require('../filterInvalidDictionaries.cjs');
const require_processContentDeclaration = require('../buildIntlayerDictionary/processContentDeclaration.cjs');
const require_getIntlayerBundle = require('./getIntlayerBundle.cjs');
let node_path = require("node:path");
let node_fs_promises = require("node:fs/promises");

//#region ../chokidar/dist/esm/loadDictionaries/loadContentDeclaration.mjs
const formatLocalDictionaries = (dictionariesRecord, configuration) => Object.entries(dictionariesRecord).filter(([_relativePath, dict]) => require_filterInvalidDictionaries.isInvalidDictionary(dict, configuration)).map(([relativePath, dict]) => ({
	...dict,
	localId: `${dict.key}::local::${relativePath}`,
	location: "local",
	filePath: relativePath
}));
const loadContentDeclarations = async (contentDeclarationFilePath, configuration, onStatusUpdate) => {
	const { build, content } = configuration;
	const { set, isValid } = require_cache.localCache(configuration, ["intlayer-bundle"], { ttlMs: 1e3 * 60 * 60 * 24 * 5 });
	const filePath = (0, node_path.join)(content.cacheDir, "intlayer-bundle.cjs");
	if (!await isValid()) {
		await (0, node_fs_promises.writeFile)(filePath, await require_getIntlayerBundle.getIntlayerBundle(configuration));
		await set("ok");
	}
	try {
		const dictionariesPromises = contentDeclarationFilePath.map(async (path) => {
			return {
				relativePath: (0, node_path.relative)(configuration.content.baseDir, path),
				dictionary: await require_loadExternalFile.loadExternalFile(path, {
					projectRequire: build.require ?? require_ESMxCJSHelpers.getProjectRequire(),
					buildOptions: { banner: { js: [`globalThis.INTLAYER_FILE_PATH = '${path}';`, `globalThis.INTLAYER_BASE_DIR = '${configuration.content.baseDir}';`].join("\n") } },
					aliases: { intlayer: filePath }
				})
			};
		});
		const contentDeclarations = formatLocalDictionaries((await Promise.all(dictionariesPromises)).reduce((acc, { relativePath, dictionary }) => {
			acc[relativePath] = dictionary;
			return acc;
		}, {}), configuration);
		const listFoundDictionaries = contentDeclarations.map((declaration) => ({
			dictionaryKey: declaration.key,
			type: "local",
			status: "found"
		}));
		onStatusUpdate?.(listFoundDictionaries);
		return require_filterInvalidDictionaries.filterInvalidDictionaries(await require_parallelize.parallelize(contentDeclarations, async (contentDeclaration) => {
			if (!contentDeclaration) return;
			onStatusUpdate?.([{
				dictionaryKey: contentDeclaration.key,
				type: "local",
				status: "building"
			}]);
			const processedContentDeclaration = await require_processContentDeclaration.processContentDeclaration(contentDeclaration);
			if (!processedContentDeclaration) return;
			onStatusUpdate?.([{
				dictionaryKey: processedContentDeclaration.key,
				type: "local",
				status: "built"
			}]);
			return processedContentDeclaration;
		}), configuration);
	} finally {}
};

//#endregion
exports.loadContentDeclarations = loadContentDeclarations;
//# sourceMappingURL=loadContentDeclaration.cjs.map