const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.cjs');
const require_normalizePath = require('../../../../config/dist/esm/utils/normalizePath.cjs');
const require_writeFileIfChanged = require('../writeFileIfChanged.cjs');
const require_getFileHash = require('../utils/getFileHash.cjs');
let node_path = require("node:path");
let node_fs_promises = require("node:fs/promises");
let fast_glob = require("fast-glob");
fast_glob = require_rolldown_runtime.__toESM(fast_glob);

//#region ../chokidar/dist/esm/createType/createModuleAugmentation.mjs
/** Returns lines like: [Locales.FRENCH]: 1; */
const formatLocales = (locales) => locales.map((locale) => `    "${locale}": 1;`).join("\n");
/** Generate the content of the module augmentation file */
const generateTypeIndexContent = (typeFiles, configuration) => {
	const { content, internationalization } = configuration;
	const { moduleAugmentationDir } = content;
	const { locales, requiredLocales, strictMode } = internationalization;
	let fileContent = "import \"intlayer\";\n";
	const dictionariesRef = typeFiles.map((dictionaryPath) => ({
		relativePath: `./${(0, node_path.relative)(moduleAugmentationDir, dictionaryPath)}`,
		id: (0, node_path.basename)(dictionaryPath, (0, node_path.extname)(dictionaryPath)),
		hash: `_${require_getFileHash.getFileHash(dictionaryPath)}`
	}));
	for (const dictionary of dictionariesRef) fileContent += `import ${dictionary.hash} from '${dictionary.relativePath}';\n`;
	fileContent += "\n";
	const formattedDictionaryMap = dictionariesRef.map((dictionary) => `    "${dictionary.id}": typeof ${dictionary.hash};`).join("\n");
	const declared = locales;
	const requiredSanitized = requiredLocales?.length ? requiredLocales.filter((requiredLocales$1) => declared.includes(requiredLocales$1)) : declared;
	const formattedDeclaredLocales = formatLocales(declared);
	const formattedRequiredLocales = formatLocales(requiredSanitized);
	const strictKey = strictMode === "strict" ? "strict" : strictMode === "inclusive" ? "inclusive" : "loose";
	/**
	* Module augmentation that ONLY adds keys to registries.
	* No types/aliases redefined here—avoids merge conflicts.
	*/
	fileContent += `declare module 'intlayer' {\n`;
	fileContent += `  interface __DictionaryRegistry {\n${formattedDictionaryMap}\n  }\n\n`;
	fileContent += `  interface __DeclaredLocalesRegistry {\n${formattedDeclaredLocales}\n  }\n\n`;
	fileContent += `  interface __RequiredLocalesRegistry {\n${formattedRequiredLocales}\n  }\n\n`;
	fileContent += `  interface __StrictModeRegistry { mode: '${strictKey}' }\n`;
	fileContent += `}\n`;
	return fileContent;
};
/** Generate the index file merging all the types */
const createModuleAugmentation = async (configuration) => {
	const { moduleAugmentationDir, typesDir } = configuration.content;
	await (0, node_fs_promises.mkdir)(moduleAugmentationDir, { recursive: true });
	const tsContent = generateTypeIndexContent(await (0, fast_glob.default)(require_normalizePath.normalizePath(`${typesDir}/*.ts`), { ignore: ["**/*.d.ts"] }), configuration);
	await require_writeFileIfChanged.writeFileIfChanged((0, node_path.join)(moduleAugmentationDir, "intlayer.d.ts"), tsContent);
};

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