import { createLogger } from "../utils/logger/index.js";
import { init_logger } from "../utils/logger.js";
import { commandRegistry } from "./registry.js";
import { getRoutingInfo } from "./session-key.js";
import { createCommandContext } from "./context.js";
//#region src/chat-commands/processor.ts
init_logger();
const log = createLogger("CommandProcessor");
/**
* Process a unified message
* Returns true if the message was handled (command or otherwise), false if should continue to AI
*/
async function processMessage(message, deps) {
	if (message.isCommand && message.commandName) return executeCommand(message, deps);
	return false;
}
/**
* Execute a command from a unified message
*/
async function executeCommand(message, deps) {
	const { commandName, commandArgs } = message;
	if (!commandName) return false;
	log.info({
		command: commandName,
		sessionKey: message.sessionKey
	}, "Executing command");
	const ctx = await createCommandContextFromMessage(message, deps);
	const result = await commandRegistry.execute(commandName, ctx, commandArgs || "");
	if (result.content) {
		const routing = getRoutingInfo(message.sessionKey);
		await deps.bus.publishOutbound({
			channel: routing.channel,
			chat_id: routing.chatId,
			content: result.content,
			type: "message",
			metadata: { threadId: routing.threadId }
		});
	}
	return true;
}
/**
* Create a command context from a unified message
*/
async function createCommandContextFromMessage(message, deps) {
	const routing = getRoutingInfo(message.sessionKey);
	return createCommandContext({
		sessionKey: message.sessionKey,
		source: message.source,
		channelId: message.channelId,
		chatId: message.chatId,
		senderId: message.senderId,
		isGroup: message.platformData.isGroup,
		config: deps.config,
		bus: deps.bus,
		sessionStore: deps.sessionStore,
		replyHandler: async (text, options) => {
			await deps.bus.publishOutbound({
				channel: routing.channel,
				chat_id: routing.chatId,
				content: text,
				type: "message",
				metadata: {
					threadId: routing.threadId,
					parseMode: options?.parseMode
				}
			});
		},
		typingHandler: async (typing) => {
			await deps.bus.publishOutbound({
				channel: routing.channel,
				chat_id: routing.chatId,
				type: typing ? "typing_on" : "typing_off",
				metadata: { threadId: routing.threadId }
			});
		},
		supportedFeatures: ["markdown", "typing"],
		getCurrentModel: deps.getCurrentModel,
		switchModel: deps.switchModel,
		listModels: deps.listModels,
		getUsage: deps.getUsage
	});
}
//#endregion
export { processMessage };

//# sourceMappingURL=processor.js.map