import { buildAgentMainSessionKey, buildAgentPeerSessionKey } from "../routing/agent-session-key.js";
import { init_session_key, parseSessionKey as parseSessionKey$1 } from "../routing/session-key.js";
//#region src/chat-commands/session-key.ts
init_session_key();
function generateSessionKey(ctx) {
	const effectiveAgentId = ctx.agentId ?? "main";
	const effectiveAccountId = ctx.accountId ?? "default";
	const channel = ctx.source === "webui" ? "webchat" : ctx.source;
	if (ctx.source === "cli") {
		if (ctx.chatId === "direct" || ctx.chatId === "main") return buildAgentMainSessionKey({
			agentId: effectiveAgentId,
			mainKey: ctx.mainKey
		});
		return buildAgentPeerSessionKey({
			agentId: effectiveAgentId,
			mainKey: ctx.mainKey,
			channel: "cli",
			accountId: effectiveAccountId,
			peerKind: "direct",
			peerId: ctx.chatId,
			dmScope: "per-peer"
		});
	}
	if (!ctx.isGroup) {
		const key = buildAgentPeerSessionKey({
			agentId: effectiveAgentId,
			mainKey: ctx.mainKey,
			channel,
			accountId: effectiveAccountId,
			peerKind: "direct",
			peerId: ctx.senderId,
			identityLinks: ctx.identityLinks,
			dmScope: ctx.dmScope ?? "per-account-channel-peer"
		});
		if (ctx.threadId) return `${key}:thread:${ctx.threadId.toLowerCase()}`;
		return key;
	}
	let key = buildAgentPeerSessionKey({
		agentId: effectiveAgentId,
		mainKey: ctx.mainKey,
		channel,
		accountId: effectiveAccountId,
		peerKind: "group",
		peerId: ctx.chatId,
		identityLinks: ctx.identityLinks
	});
	if (ctx.threadId) key = `${key}:thread:${ctx.threadId.toLowerCase()}`;
	return key;
}
function parseSessionKey(sessionKey) {
	const parsed = parseSessionKey$1(sessionKey);
	if (!parsed) return {
		source: "system",
		type: "other",
		chatId: "unknown"
	};
	let type;
	switch (parsed.peerKind) {
		case "dm":
		case "direct":
			type = parsed.peerKind === "direct" && parsed.peerId === "main" ? "direct" : "dm";
			break;
		case "group":
		case "channel":
			type = parsed.threadId ? "thread" : "group";
			break;
		default: type = "other";
	}
	return {
		source: parsed.source,
		type,
		chatId: parsed.peerId,
		threadId: parsed.threadId,
		agentId: parsed.agentId,
		accountId: parsed.accountId
	};
}
function isValidSessionKey(sessionKey) {
	return parseSessionKey$1(sessionKey) !== null;
}
function getSessionDisplayName(sessionKey) {
	const parsed = parseSessionKey(sessionKey);
	switch (parsed.type) {
		case "dm": return `Private Chat (${parsed.source})`;
		case "group": return `Group (${parsed.source})`;
		case "thread": return `Thread (${parsed.source})`;
		case "direct": return parsed.chatId === "main" ? "Main session" : `Direct (${parsed.source})`;
		default: return `${parsed.source}:${parsed.chatId}`;
	}
}
function getRoutingInfo(sessionKey) {
	const parsed = parseSessionKey(sessionKey);
	return {
		channel: parsed.source,
		chatId: parsed.chatId,
		threadId: parsed.threadId
	};
}
//#endregion
export { generateSessionKey, getRoutingInfo, getSessionDisplayName, isValidSessionKey, parseSessionKey };

//# sourceMappingURL=session-key.js.map