import { __require } from "./_virtual/rolldown_runtime.mjs";
import { spawn } from "node:child_process";
import { dirname, resolve } from "node:path";
import { createRequire } from "node:module";
import { isESModule } from "@intlayer/config";

//#region ../@intlayer/cli/dist/esm/editor.mjs
const startEditor = (options) => {
	const args = ["start"];
	if (options.env) args.push("--env", options.env);
	if (options.envFile) args.push("--env-file", options.envFile);
	const spawnNodeProcess = (binPath) => spawn(process.execPath, [binPath, ...args], {
		stdio: "inherit",
		env: { ...process.env }
	});
	const spawnWith = (cmd, cmdArgs) => spawn(cmd, cmdArgs, {
		stdio: "inherit",
		env: { ...process.env }
	});
	try {
		const child = spawnNodeProcess(resolve(dirname((isESModule ? createRequire(import.meta.url) : __require).resolve("intlayer-editor/package.json")), "bin", "intlayer-editor.mjs"));
		child.on("error", () => runFallback());
		child.on("exit", (code) => {
			if (code === 255) process.exit(code ?? 0);
		});
	} catch {
		runFallback();
	}
	function runFallback() {
		spawnWith("bun", [
			"dlx",
			"intlayer-editor",
			...args
		]).on("error", () => {
			spawnWith("npx", [
				"-y",
				"intlayer-editor",
				...args
			]).on("error", (err) => {
				console.error("Unable to execute intlayer-editor via bun or npx.");
				console.error(String(err?.message ?? err));
				process.exit(1);
			});
		});
	}
};

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