import { randomUUID } from 'node:crypto';
export function createSessionStore(config) {
    const sessions = new Map();
    const isExpired = (session, now) => {
        return now - session.lastSeenAt > config.idleTtlMs || now > session.expiresAt;
    };
    const cleanupExpiredSessions = (now) => {
        for (const [sessionId, session] of sessions.entries()) {
            if (isExpired(session, now)) {
                sessions.delete(sessionId);
            }
        }
    };
    const store = {
        create() {
            const now = Date.now();
            cleanupExpiredSessions(now);
            const session = {
                sessionId: randomUUID(),
                createdAt: now,
                lastSeenAt: now,
                expiresAt: now + config.absoluteTtlMs,
            };
            sessions.set(session.sessionId, session);
            return session;
        },
        get(sessionId) {
            const now = Date.now();
            cleanupExpiredSessions(now);
            const session = sessions.get(sessionId);
            if (!session) {
                return null;
            }
            if (isExpired(session, now)) {
                sessions.delete(sessionId);
                return null;
            }
            return session;
        },
        touch(sessionId) {
            const now = Date.now();
            cleanupExpiredSessions(now);
            const session = sessions.get(sessionId);
            if (!session) {
                return null;
            }
            if (isExpired(session, now)) {
                sessions.delete(sessionId);
                return null;
            }
            session.lastSeenAt = now;
            sessions.set(sessionId, session);
            return session;
        },
        delete(sessionId) {
            sessions.delete(sessionId);
        },
    };
    return store;
}
//# sourceMappingURL=session-store.js.map