import { c as parseAgentSessionKey } from "./session-key-utils-dXMHZk48.js";
import { o as resolveSessionStorePathCore } from "./paths-DVAvlIOc.js";
import { a as listSessionEntriesReadOnly } from "./session-accessor.sqlite-entry-CoLie3L_.js";
import { r as resolveSqliteTargetFromSessionStorePath } from "./session-sqlite-target-Cc4hugKW.js";
import "./session-accessor-Dwfzn6Ej.js";
//#region src/status/session-stores.ts
/** One collection owns the snapshot; borrowed read policies must finish before an await. */
function createStatusSessionStoreReader(readEntries = listSessionEntriesReadOnly) {
	const stores = /* @__PURE__ */ new Map();
	return {
		stores,
		read(storePath, agentId) {
			const path = resolveSqliteTargetFromSessionStorePath(storePath, { agentId }).path;
			let store = stores.get(path);
			if (!store) {
				store = {
					sessions: [],
					byAgent: /* @__PURE__ */ new Map()
				};
				for (const row of readEntries({
					...agentId ? { agentId } : {},
					storePath
				})) {
					const owner = parseAgentSessionKey(row.sessionKey)?.agentId;
					if (!owner) continue;
					store.sessions.push(row);
					const agentSessions = store.byAgent.get(owner);
					if (agentSessions) agentSessions.push(row);
					else store.byAgent.set(owner, [row]);
				}
				stores.set(path, store);
			}
			return {
				path,
				sessions: agentId ? store.byAgent.get(agentId) ?? [] : store.sessions
			};
		}
	};
}
/** Reads each physical store once, retaining retired agent namespaces in the aggregate. */
function readStatusSessionStores(cfg, agents) {
	const reader = createStatusSessionStoreReader();
	const byAgent = agents.map((agent) => ({
		agent,
		...reader.read(resolveSessionStorePathCore(cfg.session?.store, { agentId: agent.id }), agent.id)
	}));
	return {
		paths: [...reader.stores.keys()],
		sessions: [...reader.stores.values()].flatMap((store) => store.sessions),
		byAgent
	};
}
//#endregion
export { readStatusSessionStores as n, createStatusSessionStoreReader as t };
