import { t as __exportAll } from "./rolldown-runtime-Cbj13DAv.js";
import { t as isTruthyEnvValue } from "./env-Dlz8aQHP.js";
import { Mt as hasHelpOrVersion, Tt as buildParseArgv, kt as getPrimaryCommand } from "./entry.js";
import { r as resolveActionArgs } from "./helpers-DPUZzP-1.js";

//#region src/cli/program/action-reparse.ts
async function reparseProgramFromActionArgs(program, actionArgs) {
	const actionCommand = actionArgs.at(-1);
	const rawArgs = (actionCommand?.parent ?? program).rawArgs;
	const actionArgsList = resolveActionArgs(actionCommand);
	const fallbackArgv = actionCommand?.name() ? [actionCommand.name(), ...actionArgsList] : actionArgsList;
	const parseArgv = buildParseArgv({
		programName: program.name(),
		rawArgs,
		fallbackArgv
	});
	await program.parseAsync(parseArgv);
}

//#endregion
//#region src/cli/program/command-tree.ts
function removeCommand(program, command) {
	const commands = program.commands;
	const index = commands.indexOf(command);
	if (index < 0) return false;
	commands.splice(index, 1);
	return true;
}
function removeCommandByName(program, name) {
	const existing = program.commands.find((command) => command.name() === name);
	if (!existing) return false;
	return removeCommand(program, existing);
}

//#endregion
//#region src/cli/program/register.subclis.ts
var register_subclis_exports = /* @__PURE__ */ __exportAll({
	getSubCliCommandsWithSubcommands: () => getSubCliCommandsWithSubcommands,
	getSubCliEntries: () => getSubCliEntries,
	registerSubCliByName: () => registerSubCliByName,
	registerSubCliCommands: () => registerSubCliCommands
});
const shouldRegisterPrimaryOnly = (argv) => {
	if (isTruthyEnvValue(process.env.FASED_DISABLE_LAZY_SUBCOMMANDS)) return false;
	if (hasHelpOrVersion(argv)) return false;
	return true;
};
const shouldEagerRegisterSubcommands = (_argv) => {
	return isTruthyEnvValue(process.env.FASED_DISABLE_LAZY_SUBCOMMANDS);
};
const loadConfig = async () => {
	return (await import("./env-Dlz8aQHP.js").then((n) => n.qt)).loadConfig();
};
const entries = [
	{
		name: "start",
		description: "Start runtime in gateway or managed mode",
		hasSubcommands: false,
		register: async (program) => {
			(await import("./register.start-B4Zxwyvz.js")).registerStartCommand(program);
		}
	},
	{
		name: "managed",
		description: "Managed public startup helpers",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./register.managed-Di5EtWnZ.js")).registerManagedCommand(program);
		}
	},
	{
		name: "acp",
		description: "Agent Control Protocol tools",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./acp-cli-Bvzq9ZDi.js")).registerAcpCli(program);
		}
	},
	{
		name: "gateway",
		description: "Run, inspect, and query the WebSocket Gateway",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./gateway-cli-BXl9lmch.js")).registerGatewayCli(program);
		}
	},
	{
		name: "mining",
		description: "Operate SAT mining from the CLI",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./mining-cli-BAcHqmtk.js")).registerMiningCli(program);
		}
	},
	{
		name: "sat",
		description: "SAT protocol operator tools",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./sat-cli-D_9Opx_k.js")).registerSatCli(program);
		}
	},
	{
		name: "federation",
		description: "Inspect federation and hosted routing state",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./federation-cli-Ux-vcUJQ.js")).registerFederationCli(program);
		}
	},
	{
		name: "daemon",
		description: "Gateway service (legacy alias)",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./daemon-cli-DFjCvbUo.js").then((n) => n.t)).registerDaemonCli(program);
		}
	},
	{
		name: "logs",
		description: "Tail gateway file logs via RPC",
		hasSubcommands: false,
		register: async (program) => {
			(await import("./logs-cli-DH7gpUMO.js")).registerLogsCli(program);
		}
	},
	{
		name: "system",
		description: "System events, heartbeat, and presence",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./system-cli-OaR87ANR.js")).registerSystemCli(program);
		}
	},
	{
		name: "models",
		description: "Discover, scan, and configure models",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./models-cli-CXdCnyks.js")).registerModelsCli(program);
		}
	},
	{
		name: "providers",
		description: "Review provider manifests and model catalogs",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./providers-cli-C9g9sMPH.js")).registerProvidersCli(program);
		}
	},
	{
		name: "approvals",
		description: "Manage exec approvals (gateway or node host)",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./exec-approvals-cli-CbhJsKEB.js")).registerExecApprovalsCli(program);
		}
	},
	{
		name: "nodes",
		description: "Manage gateway-owned node pairing and node commands",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./nodes-cli-CAPteESQ.js")).registerNodesCli(program);
		}
	},
	{
		name: "devices",
		description: "Device pairing + token management",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./devices-cli-CdBxoOPM.js")).registerDevicesCli(program);
		}
	},
	{
		name: "node",
		description: "Run and manage the headless node host service",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./node-cli-DkgKrYS1.js")).registerNodeCli(program);
		}
	},
	{
		name: "sandbox",
		description: "Manage sandbox containers for agent isolation",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./sandbox-cli-DHHAXWD8.js")).registerSandboxCli(program);
		}
	},
	{
		name: "tui",
		description: "Open a terminal UI connected to the Gateway",
		hasSubcommands: false,
		register: async (program) => {
			(await import("./tui-cli-z5WV3SVg.js")).registerTuiCli(program);
		}
	},
	{
		name: "task",
		description: "Manage scheduled Agent tasks",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./cron-cli-DYWW7cv1.js")).registerTaskCli(program);
		}
	},
	{
		name: "cron",
		description: "Manage scheduled Agent tasks (compatibility alias)",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./cron-cli-DYWW7cv1.js")).registerCronCli(program, {
				commandName: "cron",
				description: "Manage scheduled Agent tasks (compatibility alias)",
				docsPath: "/cli/task",
				docsLabel: "docs.fased.ai/cli/task"
			});
		}
	},
	{
		name: "dns",
		description: "DNS helpers for wide-area discovery (Tailscale + CoreDNS)",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./dns-cli-C1ggDCID.js")).registerDnsCli(program);
		}
	},
	{
		name: "docs",
		description: "Search the live FasedAgent docs",
		hasSubcommands: false,
		register: async (program) => {
			(await import("./docs-cli-MSvmFEv1.js")).registerDocsCli(program);
		}
	},
	{
		name: "hooks",
		description: "Manage internal agent hooks",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./hooks-cli-BDpBNlPV.js")).registerHooksCli(program);
		}
	},
	{
		name: "webhooks",
		description: "Webhook helpers and integrations",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./webhooks-cli-Bk4Lkkpk.js")).registerWebhooksCli(program);
		}
	},
	{
		name: "qr",
		description: "Generate iOS pairing QR/setup code",
		hasSubcommands: false,
		register: async (program) => {
			(await import("./qr-cli-B0QuJjDI.js")).registerQrCli(program);
		}
	},
	{
		name: "pairing",
		description: "Secure DM pairing (approve inbound requests)",
		hasSubcommands: true,
		register: async (program) => {
			const { registerPluginCliCommands } = await import("./cli-CE8pVYeA.js");
			registerPluginCliCommands(program, await loadConfig());
			(await import("./pairing-cli-XJJp527O.js")).registerPairingCli(program);
		}
	},
	{
		name: "plugins",
		description: "Manage FasedAgent plugins and extensions",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./plugins-cli-Dj-p00ob.js")).registerPluginsCli(program);
			const { registerPluginCliCommands } = await import("./cli-CE8pVYeA.js");
			registerPluginCliCommands(program, await loadConfig());
		}
	},
	{
		name: "channels",
		description: "Manage connected chat channels (Telegram, Discord, etc.)",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./channels-cli-CNhEFKbn.js")).registerChannelsCli(program);
		}
	},
	{
		name: "directory",
		description: "Lookup contact and group IDs (self, peers, groups) for supported chat channels",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./directory-cli-D0A5TwnY.js")).registerDirectoryCli(program);
		}
	},
	{
		name: "security",
		description: "Security tools and local config audits",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./security-cli-CAEEYR8C.js")).registerSecurityCli(program);
		}
	},
	{
		name: "secrets",
		description: "Secrets runtime reload controls",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./secrets-cli-DuPkN04v.js")).registerSecretsCli(program);
		}
	},
	{
		name: "skills",
		description: "List and inspect available skills",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./skills-cli-CrXKTpsn.js")).registerSkillsCli(program);
		}
	},
	{
		name: "update",
		description: "Update FasedAgent and inspect update channel status",
		hasSubcommands: true,
		register: async (program) => {
			(await import("./update-cli-BdQ3mRGs.js")).registerUpdateCli(program);
		}
	},
	{
		name: "completion",
		description: "Generate shell completion script",
		hasSubcommands: false,
		register: async (program) => {
			(await import("./completion-cli-CKzB1lB1.js").then((n) => n.n)).registerCompletionCli(program);
		}
	}
];
function getSubCliEntries() {
	return entries;
}
function getSubCliCommandsWithSubcommands() {
	return entries.filter((entry) => entry.hasSubcommands).map((entry) => entry.name);
}
async function registerSubCliByName(program, name) {
	const entry = entries.find((candidate) => candidate.name === name);
	if (!entry) return false;
	removeCommandByName(program, entry.name);
	await entry.register(program);
	return true;
}
function registerLazyCommand(program, entry) {
	const placeholder = program.command(entry.name).description(entry.description);
	placeholder.allowUnknownOption(true);
	placeholder.allowExcessArguments(true);
	placeholder.action(async (...actionArgs) => {
		removeCommand(program, placeholder);
		await entry.register(program);
		await reparseProgramFromActionArgs(program, actionArgs);
	});
}
function registerSubCliCommands(program, argv = process.argv) {
	if (shouldEagerRegisterSubcommands(argv)) {
		for (const entry of entries) entry.register(program);
		return;
	}
	const primary = getPrimaryCommand(argv);
	if (primary && shouldRegisterPrimaryOnly(argv)) {
		const entry = entries.find((candidate) => candidate.name === primary);
		if (entry) {
			registerLazyCommand(program, entry);
			return;
		}
	}
	for (const candidate of entries) registerLazyCommand(program, candidate);
}

//#endregion
export { register_subclis_exports as a, registerSubCliCommands as i, getSubCliEntries as n, removeCommandByName as o, registerSubCliByName as r, reparseProgramFromActionArgs as s, getSubCliCommandsWithSubcommands as t };