import { resolveCliCatalogCommandPath } from "./command-path-policy.js";
import { shouldLoadExtensionCliForCommandPath } from "./command-startup-policy.js";
import { registry } from "./registry.js";
//#region src/cli/command-loaders.ts
const REGISTRY_COMMAND_MODULES = {
	init: () => import("./commands/init.js"),
	profile: () => import("./commands/profile.js"),
	setup: () => import("./commands/setup.js"),
	onboard: () => import("./commands/onboard.js"),
	agent: () => import("./commands/agent.js"),
	tui: () => import("./commands/tui.js"),
	gateway: () => import("./commands/gateway.js"),
	session: () => import("./commands/session.js"),
	cron: () => import("./commands/cron.js"),
	config: () => import("./commands/config.js"),
	doctor: () => import("./commands/doctor/index.js"),
	image: () => import("./commands/image.js"),
	channels: () => import("./commands/channels.js"),
	models: () => import("./commands/models.js"),
	providers: () => import("./commands/providers.js"),
	voice: () => import("./commands/voice.js"),
	search: () => import("./commands/search.js"),
	auth: () => import("./commands/auth.js"),
	skills: () => import("./commands/skills.js"),
	browser: () => import("./commands/browser.js"),
	update: () => import("./commands/update.js"),
	logs: () => import("./commands/logs.js"),
	tunnel: () => import("./commands/tunnel.js"),
	tailscale: () => import("./commands/tailscale.js"),
	mcp: () => import("./commands/mcp.js")
};
const NON_REGISTRY_COMMAND_MATCHERS = [{
	matches: (name) => name === "agents" || name.startsWith("agents:"),
	load: async (program) => {
		const { registerAgentsCli } = await import("./commands/agents.js");
		registerAgentsCli(program);
	}
}, {
	matches: (name) => name === "extensions",
	load: async (program) => {
		const { registerExtensionCommands } = await import("./commands/extension.js");
		registerExtensionCommands(program);
		if (shouldLoadExtensionCliForCommandPath(resolveCliCatalogCommandPath(process.argv))) {
			const { registerExtensionCliCommands } = await import("./extension-cli-register.js");
			await registerExtensionCliCommands(program);
		}
	}
}];
const FLAGS_WITH_VALUE = new Set(["--config", "--workspace"]);
/**
* Resolve which command module to load based on argv. Skips global flags
* (`--verbose`/`--config <path>`/`--workspace <path>`) and treats
* `help <cmd>` as if the user had typed `<cmd>`, so that `xopc help gateway`
* loads only the gateway module.
*/
function resolveCommandName(argv) {
	let i = 2;
	let firstSubcommand;
	while (i < argv.length) {
		const arg = argv[i];
		if (!arg) {
			i += 1;
			continue;
		}
		if (arg.startsWith("-")) {
			i += FLAGS_WITH_VALUE.has(arg) ? 2 : 1;
			continue;
		}
		if (firstSubcommand === void 0) {
			firstSubcommand = arg;
			if (arg === "help") {
				i += 1;
				continue;
			}
			return arg;
		}
		return arg;
	}
	return firstSubcommand;
}
async function tryLoadCommand(program, ctx, name, getCtx) {
	const moduleLoader = REGISTRY_COMMAND_MODULES[name];
	if (moduleLoader) {
		await moduleLoader();
		const installed = registry.installOne(program, name, ctx, getCtx);
		if (installed && name === "gateway") {
			const gatewayCmd = program.commands.find((command) => command.name() === "gateway");
			if (gatewayCmd) {
				const { prepareGatewayCommandForArgv } = await import("./commands/gateway.js");
				await prepareGatewayCommandForArgv(gatewayCmd, ctx);
			}
		}
		return installed;
	}
	const matcher = NON_REGISTRY_COMMAND_MATCHERS.find((m) => m.matches(name));
	if (matcher) {
		await matcher.load(program);
		return true;
	}
	return false;
}
async function loadAllCommands(program, ctx, getCtx) {
	for (const loader of Object.values(REGISTRY_COMMAND_MODULES)) await loader();
	registry.install(program, ctx, getCtx);
	for (const matcher of NON_REGISTRY_COMMAND_MATCHERS) await matcher.load(program);
	const gatewayCmd = program.commands.find((command) => command.name() === "gateway");
	if (gatewayCmd) {
		const { prepareGatewayCommandForArgv } = await import("./commands/gateway.js");
		await prepareGatewayCommandForArgv(gatewayCmd, ctx);
	}
}
//#endregion
export { NON_REGISTRY_COMMAND_MATCHERS, REGISTRY_COMMAND_MODULES, loadAllCommands, resolveCommandName, tryLoadCommand };

//# sourceMappingURL=command-loaders.js.map