import { init_workspace_defaults, resolveDefaultAgentWorkspaceDir } from "../config/workspace-defaults.js";
import { init_paths, resolveConfigPath } from "../config/paths.js";
//#region src/cli/registry.ts
init_paths();
init_workspace_defaults();
function createDefaultContext(argv = process.argv, opts) {
	return {
		configPath: opts?.config || process.env.XOPC_CONFIG || resolveConfigPath(),
		workspacePath: opts?.workspace || process.env.XOPC_WORKSPACE || resolveDefaultAgentWorkspaceDir(),
		isVerbose: opts?.verbose ?? (argv.includes("--verbose") || argv.includes("-v")),
		argv
	};
}
var CommandRegistry = class {
	commands = [];
	initialized = false;
	suppressLateRegistrationWarnings = false;
	/**
	* Temporarily silence the "registered after initialization" warning. Used by
	* commands that need to bulk-load other command modules to harvest their
	* side effects during initialization.
	*/
	setSuppressLateRegistrationWarnings(value) {
		this.suppressLateRegistrationWarnings = value;
	}
	register(def) {
		if (this.initialized && !this.suppressLateRegistrationWarnings) console.warn(`Warning: Command "${def.id}" registered after initialization`);
		const existing = this.commands.find((c) => c.id === def.id);
		if (existing) throw new Error(`Command with id "${def.id}" already registered (name: ${existing.name})`);
		const existingName = this.commands.find((c) => c.name === def.name);
		if (existingName) throw new Error(`Command with name "${def.name}" already registered (id: ${existingName.id})`);
		this.commands.push(def);
	}
	registerAll(defs) {
		for (const def of defs) this.register(def);
	}
	getCommands() {
		return this.commands;
	}
	getCommandsByCategory(category) {
		return this.commands.filter((c) => c.metadata?.category === category);
	}
	findById(id) {
		return this.commands.find((c) => c.id === id);
	}
	findByName(name) {
		return this.commands.find((c) => c.name === name);
	}
	install(program, ctx, getCtx) {
		this.initialized = true;
		const categoryOrder = {
			setup: 0,
			runtime: 1,
			maintenance: 2,
			utility: 3
		};
		const sorted = [...this.commands].sort((a, b) => {
			return categoryOrder[a.metadata?.category ?? "utility"] - categoryOrder[b.metadata?.category ?? "utility"];
		});
		for (const def of sorted) {
			if (def.metadata?.hidden) continue;
			try {
				const cmd = def.factory(ctx, getCtx);
				program.addCommand(cmd);
			} catch (error) {
				console.error(`Failed to register command "${def.id}":`, error);
			}
		}
	}
	installOne(program, name, ctx, getCtx) {
		this.initialized = true;
		const def = this.findByName(name);
		if (!def || def.metadata?.hidden) return false;
		try {
			const cmd = def.factory(ctx, getCtx);
			program.addCommand(cmd);
			return true;
		} catch (error) {
			console.error(`Failed to register command "${def.id}":`, error);
			return false;
		}
	}
	getStats() {
		const byCategory = {};
		for (const cmd of this.commands) {
			const cat = cmd.metadata?.category ?? "uncategorized";
			byCategory[cat] = (byCategory[cat] || 0) + 1;
		}
		return {
			total: this.commands.length,
			byCategory
		};
	}
};
const registry = new CommandRegistry();
function register(def) {
	registry.register(def);
}
function formatExamples(examples) {
	if (examples.length === 0) return "";
	return "\nExamples:\n" + examples.map((e) => `  $ ${e}`).join("\n");
}
//#endregion
export { CommandRegistry, createDefaultContext, formatExamples, register, registry };

//# sourceMappingURL=registry.js.map