import { getFileHash } from "../utils/getFileHash.mjs";
import { basename, extname, relative } from "node:path";
import { getConfiguration, normalizePath } from "@intlayer/config";

//#region ../@intlayer/chokidar/dist/esm/createDictionaryEntryPoint/generateDictionaryListContent.mjs
/**
* This function generates the content of the dictionary list file
*/
const generateDictionaryListContent = (dictionaries, functionName, format = "esm", configuration = getConfiguration()) => {
	const { mainDir } = configuration.content;
	let content = "";
	const dictionariesRef = dictionaries.map((dictionaryPath) => ({
		relativePath: normalizePath(relative(mainDir, dictionaryPath)),
		id: basename(dictionaryPath, extname(dictionaryPath)),
		hash: `_${getFileHash(dictionaryPath)}`
	}));
	dictionariesRef.forEach((dictionary) => {
		if (format === "esm") content += `import ${dictionary.hash} from '${dictionary.relativePath}' with { type: 'json' };\n`;
		if (format === "cjs") content += `const ${dictionary.hash} = require('${dictionary.relativePath}');\n`;
	});
	content += "\n";
	const formattedDictionaryMap = dictionariesRef.map((dictionary) => `  "${dictionary.id}": ${dictionary.hash}`).join(",\n");
	content += `const dictionaries = {\n${formattedDictionaryMap}\n};\n`;
	content += `const ${functionName} = () => dictionaries;\n`;
	if (format === "esm") {
		content += `\n`;
		content += `export { ${functionName} };\n`;
		content += `export default dictionaries;\n`;
	}
	if (format === "cjs") {
		content += `\n`;
		content += `module.exports.${functionName} = ${functionName};\n`;
		content += `module.exports = dictionaries;\n`;
	}
	return content;
};

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