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');
let node_child_process = require("node:child_process");
let node_path = require("node:path");
let node_module = require("node:module");

//#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) => (0, node_child_process.spawn)(process.execPath, [binPath, ...args], {
		stdio: "inherit",
		env: { ...process.env }
	});
	const spawnWith = (cmd, cmdArgs) => (0, node_child_process.spawn)(cmd, cmdArgs, {
		stdio: "inherit",
		env: { ...process.env }
	});
	try {
		const child = spawnNodeProcess((0, node_path.resolve)((0, node_path.dirname)((require_ESMxCJSHelpers.isESModule ? (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href) : require_rolldown_runtime$1.__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
exports.startEditor = startEditor;
//# sourceMappingURL=editor.cjs.map