import { getPackageJsonPath } from "../utils/getPackageJsonPath.mjs";
import { getLoader } from "./bundleFile.mjs";
import { dirname, extname, join } from "node:path";
import { buildSync } from "esbuild";
import { pathToFileURL } from "node:url";

//#region ../@intlayer/cli/dist/esm/chokidar/dist/esm/remote-dictionaries-entry/dist/esm/config/dist/esm/loadExternalFile/transpileTSToMJS.mjs
const getTransformationOptions = (filePath) => ({
	loader: {
		".js": "js",
		".jsx": "jsx",
		".mjs": "js",
		".ts": "ts",
		".tsx": "tsx",
		".cjs": "js",
		".json": "json",
		".md": "text",
		".mdx": "text"
	},
	format: "cjs",
	target: "node20",
	platform: "node",
	write: false,
	packages: "external",
	bundle: true,
	tsconfig: join(getPackageJsonPath(dirname(filePath)).baseDir, "tsconfig.json"),
	define: { "import.meta.url": JSON.stringify(pathToFileURL(filePath).href) }
});
const transpileTSToMJSSync = (code, filePath, options) => {
	return buildSync({
		stdin: {
			contents: code,
			loader: getLoader(extname(filePath)),
			resolveDir: dirname(filePath),
			sourcefile: filePath
		},
		...getTransformationOptions(filePath),
		...options
	}).outputFiles?.[0].text;
};

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