const require_rolldown_runtime = require('../../../../../../../../_virtual/rolldown_runtime.cjs');
const require_rolldown_runtime$1 = require('../_virtual/rolldown_runtime.cjs');
const require_ESMxCJSHelpers = require('../../../../../../../config/dist/esm/utils/ESMxCJSHelpers.cjs');
const require_bundleFile = require('../../../../../../../config/dist/esm/loadExternalFile/bundleFile.cjs');
let node_path = require("node:path");
let node_fs_promises = require("node:fs/promises");
let node_module = require("node:module");

//#region ../@intlayer/cli/dist/esm/chokidar/dist/esm/loadDictionaries/getIntlayerBundle.mjs
/**
* Rewrites selected bare specifiers (and any of their subpaths) to absolute file paths,
* using the provided localeRequire (either createRequire(import.meta.url) or require).
*
* Example:
*   rewritePathsPlugin(["@intlayer/config", "@intlayer/core"], localeRequire)
* …will also rewrite "@intlayer/core/file" etc.
*/
const rewritePathsPlugin = (replaceModules, excludeModules) => {
	return {
		name: "rewrite-paths",
		setup(build) {
			build.onResolve({ filter: /.*/ }, (args) => {
				const exact = replaceModules[args.path];
				if (excludeModules?.includes(args.path)) return null;
				if (exact) return {
					path: exact,
					namespace: "intlayer-replace-modules",
					external: true
				};
				for (const key of Object.keys(replaceModules)) if (args.path === key || args.path.startsWith(`${key}/`)) {
					const sub = args.path.slice(key.length);
					return {
						path: replaceModules[key] + sub,
						namespace: "intlayer-replace-modules",
						external: true
					};
				}
			});
		}
	};
};
/**
* Get the intlayer bundle to embed @intlayer/core and be able to mock @intlayer/config/built to mock the configuration file.
*/
const getIntlayerBundle = async (configuration) => {
	const rootRequire = require_ESMxCJSHelpers.getProjectRequire(configuration.content.baseDir);
	const configPackageRequire = require_ESMxCJSHelpers.configESMxCJSRequire;
	const localRequire = require_ESMxCJSHelpers.isESModule ? (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href) : require_rolldown_runtime$1.__require;
	const configurationPath = (0, node_path.join)(configuration.content.configDir, `configuration.json`);
	const replaceModules = {
		deepmerge: localRequire.resolve("deepmerge"),
		esbuild: configPackageRequire.resolve("esbuild"),
		"@intlayer/config/built": configurationPath,
		"@intlayer/config": localRequire.resolve("@intlayer/config"),
		"@intlayer/config/client": localRequire.resolve("@intlayer/config/client"),
		"@intlayer/core/file": localRequire.resolve("@intlayer/core/file")
	};
	const filePath = rootRequire.resolve("intlayer");
	return await require_bundleFile.bundleFile(await (0, node_fs_promises.readFile)(filePath, "utf-8"), filePath, {
		external: [...node_module.builtinModules, ...node_module.builtinModules.map((mod) => `node:${mod}`)],
		minify: true,
		plugins: [rewritePathsPlugin(replaceModules)]
	}) ?? "";
};

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