import { n as resolveConfig, t as merge } from "./config-FJViU9hJ.mjs";
import { defineCommand } from "citty";
import { existsSync } from "node:fs";
import { resolve } from "node:path";
import { createServer } from "vite";
import { fileURLToPath } from "node:url";
//#region src/commands/editor.ts
function findEditorRoot(startDir) {
	if (existsSync(resolve(startDir, "editor", "index.html"))) return resolve(startDir, "editor");
	let dir = startDir;
	for (let i = 0; i < 10; i++) {
		if (existsSync(resolve(dir, "apps/editor/index.html"))) return resolve(dir, "apps/editor");
		dir = resolve(dir, "..");
	}
	throw new Error("Could not find apps/editor relative to CLI package");
}
var editor_default = defineCommand({
	meta: {
		name: "editor",
		description: "Start the plan editor in dev mode with HMR"
	},
	args: {
		port: {
			type: "string",
			description: "Port to listen on",
			default: "5173"
		},
		host: {
			type: "string",
			description: "Host to listen on",
			default: "localhost"
		}
	},
	async run({ args }) {
		const cwd = process.cwd();
		const config = await resolveConfig(cwd);
		process.env.PLANNAR_CWD = cwd;
		process.env.PLANNAR_FOLDER = config.plannarFolder;
		process.env.PLANNAR_EXPORTS_FOLDER = config.exportsFolder;
		if (config.globalCss) process.env.PLANNAR_GLOBAL_CSS = resolve(cwd, config.globalCss);
		if (config.cssFilePath) process.env.PLANNAR_CSS_FILE_PATH = resolve(cwd, config.cssFilePath);
		if (config.meta) process.env.PLANNAR_META = JSON.stringify(config.meta);
		const editorRoot = findEditorRoot(fileURLToPath(new URL("..", import.meta.url)));
		const baseServerConfig = {
			configFile: resolve(editorRoot, "vite.config.ts"),
			root: editorRoot,
			server: {
				fs: { allow: [editorRoot, cwd] },
				port: Number(args.port),
				host: args.host
			}
		};
		const server = await createServer(config.viteConfig?.editor ? merge(baseServerConfig, config.viteConfig.editor) : baseServerConfig);
		await server.listen();
		server.printUrls();
	}
});
//#endregion
export { editor_default as default };
