const require_rolldown_runtime = require('../../../../../../../../../../../../../../_virtual/rolldown_runtime.cjs');
const require_ESMxCJSHelpers = require('../utils/ESMxCJSHelpers.cjs');
const require_loadEnvFile = require('../loadEnvFile.cjs');
let node_vm = require("node:vm");
let esbuild = require("esbuild");
esbuild = require_rolldown_runtime.__toESM(esbuild);

//#region ../@intlayer/webpack/dist/esm/chokidar/dist/esm/core/dist/esm/config/dist/esm/loadExternalFile/parseFileContent.mjs
const getSandBoxContext = (options) => {
	const { envVarOptions, projectRequire, additionalEnvVars, mocks, aliases } = options ?? {};
	let additionalGlobalVar = {};
	const baseRequire = typeof projectRequire === "function" ? projectRequire : require_ESMxCJSHelpers.getProjectRequire();
	const mockedRequire = (() => {
		const mockTable = Object.assign({ esbuild }, mocks);
		const aliasTable = Object.assign({}, aliases);
		const wrappedRequire = function mockableRequire(id) {
			const target = aliasTable?.[id] ? aliasTable[id] : id;
			if (mockTable && Object.hasOwn(mockTable, target)) return mockTable[target];
			if (target !== id && mockTable && Object.hasOwn(mockTable, id)) return mockTable[id];
			return baseRequire(target);
		};
		wrappedRequire.resolve = baseRequire.resolve.bind(baseRequire);
		wrappedRequire.main = baseRequire.main;
		wrappedRequire.extensions = baseRequire.extensions;
		wrappedRequire.cache = baseRequire.cache;
		return wrappedRequire;
	})();
	try {
		additionalGlobalVar = { React: baseRequire("react") };
	} catch (_err) {}
	const sandboxContext = {
		exports: { default: {} },
		module: { exports: {} },
		process: {
			...process,
			env: {
				...process.env,
				...require_loadEnvFile.loadEnvFile(envVarOptions),
				...additionalEnvVars
			}
		},
		console,
		require: mockedRequire,
		...additionalGlobalVar
	};
	Object.getOwnPropertyNames(globalThis).forEach((key) => {
		if (!(key in sandboxContext)) sandboxContext[key] = globalThis[key];
	});
	return sandboxContext;
};
const parseFileContent = (fileContentString, options) => {
	const sandboxContext = getSandBoxContext(options);
	(0, node_vm.runInNewContext)(`"use strict";\n${fileContentString}`, sandboxContext);
	const candidates = [
		sandboxContext.exports?.default,
		sandboxContext.module?.exports?.defaults,
		sandboxContext.module?.exports?.default,
		sandboxContext.module?.exports
	];
	for (const candidate of candidates) if (candidate && typeof candidate === "object" && Object.keys(candidate).length > 0) return candidate;
};

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