import { x as isSubagentSessionKey, y as isCronSessionKey } from "./session-key-NGoeHrFI.js";
import { l as resolveStorePath } from "./paths-DRORteAV.js";
import { t as loadSessionStore } from "./store-load-Cz0tEUuX.js";
//#region src/gateway/cognitive-delivery.ts
function findSessionKeyForUserId(cfg, userId) {
	if (!cfg) return;
	const store = loadSessionStore(resolveStorePath(cfg.session?.store, { agentId: "main" }));
	const directKey = `agent:main:${userId}`;
	if (store[directKey] && !isSubagentSessionKey(directKey) && !isCronSessionKey(directKey)) return directKey;
	for (const key of Object.keys(store)) {
		if (isSubagentSessionKey(key) || isCronSessionKey(key)) continue;
		if (key.endsWith(`:${userId}`)) return key;
	}
}
/**
* Resolve the full delivery target for a cognitive insight: session key,
* channel, and recipient address. Returns undefined if no routable session
* exists or if the session has no channel binding (e.g. lastChannel missing).
*/
function resolveCognitiveDeliveryTarget(cfg, userId) {
	if (!cfg) return;
	const store = loadSessionStore(resolveStorePath(cfg.session?.store, { agentId: "main" }));
	const sessionKey = findSessionKeyForUserId(cfg, userId);
	if (!sessionKey) return;
	const entry = store[sessionKey];
	if (!entry) return;
	const channel = entry.lastChannel;
	const to = entry.lastTo;
	if (!channel || channel === "none" || !to) return;
	return {
		sessionKey,
		channel,
		to,
		accountId: entry.lastAccountId ?? void 0
	};
}
//#endregion
export { resolveCognitiveDeliveryTarget };
