/**
 * Session Manager - Scoped state per MCP session
 *
 * Each session tracks its own currentDatabase, userId, and active operations.
 * Stdio transport uses a default "local" session.
 *
 * CRITICAL: NEVER use console.log() - stdout is reserved for JSON-RPC protocol.
 *
 * @module server/transports/session-manager
 */
// ═══════════════════════════════════════════════════════════════════════════════
// SESSION MANAGER
// ═══════════════════════════════════════════════════════════════════════════════
export class SessionManager {
    sessions = new Map();
    static LOCAL_SESSION_ID = 'local';
    /** Get or create session state */
    getSession(sessionId) {
        let session = this.sessions.get(sessionId);
        if (!session) {
            session = {
                sessionId,
                currentDatabase: null,
                userId: null,
                activeOperations: 0,
                createdAt: Date.now(),
                lastActivity: Date.now(),
                metadata: {},
            };
            this.sessions.set(sessionId, session);
        }
        session.lastActivity = Date.now();
        return session;
    }
    /** Get the local (stdio) session */
    getLocalSession() {
        return this.getSession(SessionManager.LOCAL_SESSION_ID);
    }
    /** Remove a session */
    removeSession(sessionId) {
        this.sessions.delete(sessionId);
    }
    /** Set user for a session */
    setSessionUser(sessionId, userId) {
        const session = this.getSession(sessionId);
        session.userId = userId;
    }
    /** Get all active sessions */
    listSessions() {
        return Array.from(this.sessions.values());
    }
    /** Get session count */
    getSessionCount() {
        return this.sessions.size;
    }
    /** Clean up expired sessions */
    cleanupExpired(ttlMs) {
        const now = Date.now();
        let cleaned = 0;
        for (const [id, session] of this.sessions) {
            if (id !== SessionManager.LOCAL_SESSION_ID && now - session.lastActivity > ttlMs) {
                this.sessions.delete(id);
                cleaned++;
            }
        }
        return cleaned;
    }
}
// ═══════════════════════════════════════════════════════════════════════════════
// SINGLETON INSTANCE
// ═══════════════════════════════════════════════════════════════════════════════
/** Singleton session manager */
export const sessionManager = new SessionManager();
//# sourceMappingURL=session-manager.js.map