import { buildAgentMainSessionKey, init_agent_session_key, normalizeMainKey } from "../routing/agent-session-key.js";
import { buildSessionKey, init_session_key, parseSessionKey } from "../routing/session-key.js";
import { getDefaultAgentId, init_resolve_route } from "../routing/resolve-route.js";
import { randomUUID } from "node:crypto";
//#region src/gateway/resolve-webchat-session-key.ts
init_agent_session_key();
init_session_key();
init_resolve_route();
/**
* Resolve the canonical `agent:` session key for webchat `/api/agent` requests.
* Rejects bare legacy peer ids (e.g. `chat_*` without `agent:` prefix).
*/
function resolveWebchatSessionKey(input) {
	const agentId = getDefaultAgentId(input.cfg);
	const mainKey = normalizeMainKey(input.cfg.session?.mainKey);
	if (input.newSession) return {
		ok: true,
		sessionKey: buildSessionKey({
			agentId,
			source: "webchat",
			accountId: "default",
			peerKind: "direct",
			peerId: `chat_${randomUUID()}`
		})
	};
	const raw = (input.sessionKey?.trim() || input.chatId?.trim() || "").trim();
	if (!raw || raw === "default") return {
		ok: true,
		sessionKey: buildAgentMainSessionKey({
			agentId,
			mainKey
		})
	};
	if (!parseSessionKey(raw)) return {
		ok: false,
		error: "sessionKey must use agent:{agentId}:{rest} format; create sessions via POST /api/sessions"
	};
	return {
		ok: true,
		sessionKey: raw
	};
}
//#endregion
export { resolveWebchatSessionKey };

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