import { createLogger } from "../utils/logger/index.js";
import { init_logger } from "../utils/logger.js";
import { normalizeTelegramCommandName, parseSlashCommand } from "./command-parse.js";
//#region src/chat-commands/registry.ts
init_logger();
const log = createLogger("CommandRegistry");
var CommandRegistry = class CommandRegistry {
	commands = /* @__PURE__ */ new Map();
	aliases = /* @__PURE__ */ new Map();
	static instance;
	static getInstance() {
		if (!CommandRegistry.instance) CommandRegistry.instance = new CommandRegistry();
		return CommandRegistry.instance;
	}
	/**
	* Register a command
	*/
	register(command) {
		if (this.commands.has(command.id)) log.warn({ commandId: command.id }, "Command already registered, overwriting");
		const existingByName = this.findByName(command.name);
		if (existingByName && existingByName.id !== command.id) log.warn({
			name: command.name,
			existingId: existingByName.id,
			newId: command.id
		}, "Command name conflict");
		this.commands.set(command.id, command);
		if (command.aliases) for (const alias of command.aliases) {
			const existingAlias = this.aliases.get(alias);
			if (existingAlias && existingAlias !== command.id) log.warn({
				alias,
				existingId: existingAlias,
				newId: command.id
			}, "Command alias conflict");
			else this.aliases.set(alias, command.id);
		}
		log.debug({
			commandId: command.id,
			name: command.name
		}, "Command registered");
	}
	/**
	* Unregister a command
	*/
	unregister(commandId) {
		const command = this.commands.get(commandId);
		if (!command) return;
		if (command.aliases) {
			for (const alias of command.aliases) if (this.aliases.get(alias) === commandId) this.aliases.delete(alias);
		}
		this.commands.delete(commandId);
		log.debug({ commandId }, "Command unregistered");
	}
	/**
	* Get command by ID
	*/
	get(commandId) {
		return this.commands.get(commandId);
	}
	/**
	* Find command by name or alias
	*/
	findByName(name) {
		for (const cmd of this.commands.values()) if (cmd.name === name) return cmd;
		const aliasedId = this.aliases.get(name);
		if (aliasedId) return this.commands.get(aliasedId);
	}
	/**
	* Check if a command exists (by name or alias)
	*/
	has(name) {
		return !!this.findByName(name);
	}
	/**
	* Get all registered commands
	*/
	list() {
		return Array.from(this.commands.values());
	}
	/**
	* Get commands by category
	*/
	listByCategory(category) {
		return this.list().filter((cmd) => cmd.category === category);
	}
	/**
	* Get commands available in a specific scope
	*/
	listByScope(scope) {
		return this.list().filter((cmd) => cmd.scope.includes(scope) || cmd.scope.includes("global"));
	}
	/**
	* Execute a command
	*/
	async execute(name, context, args = "") {
		const command = this.findByName(name);
		if (!command) return {
			content: `Unknown command: /${name}\nType /help to see available commands.`,
			success: false
		};
		const scope = context.isGroup ? "group" : "private";
		if (!command.scope.includes("global") && !command.scope.includes(scope)) return {
			content: `Command /${name} is not available in ${scope} chats.`,
			success: false
		};
		log.info({
			command: name,
			sessionKey: context.sessionKey
		}, "Executing command");
		try {
			const result = await command.handler(context, args);
			if (!result) return {
				content: "",
				success: true
			};
			return {
				content: result.content,
				success: result.success ?? true,
				components: result.components,
				metadata: result.metadata
			};
		} catch (error) {
			log.error({
				command: name,
				error
			}, "Command execution failed");
			return {
				content: `Error executing command: ${error instanceof Error ? error.message : String(error)}`,
				success: false
			};
		}
	}
	/**
	* Parse command from message text
	* Returns { command: string, args: string } or null if not a command
	*/
	parseCommand(text) {
		const trimmed = text.trim();
		if (trimmed.startsWith(":")) {
			const withoutPrefix = trimmed.slice(1);
			const spaceIndex = withoutPrefix.indexOf(" ");
			if (spaceIndex === -1) return {
				command: normalizeTelegramCommandName(withoutPrefix),
				args: ""
			};
			return {
				command: normalizeTelegramCommandName(withoutPrefix.slice(0, spaceIndex)),
				args: withoutPrefix.slice(spaceIndex + 1).trim()
			};
		}
		return parseSlashCommand(text);
	}
	/**
	* Clear all registered commands (mainly for testing)
	*/
	clear() {
		this.commands.clear();
		this.aliases.clear();
		log.debug("Command registry cleared");
	}
};
const commandRegistry = CommandRegistry.getInstance();
//#endregion
export { CommandRegistry, commandRegistry };

//# sourceMappingURL=registry.js.map