"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisSessionStore = exports.MemorySessionStore = void 0;
// ---------------------------------------------------------------------------
// In-memory implementation
// ---------------------------------------------------------------------------
class MemorySessionStore {
    store = new Map();
    async set(key, value, ttlSeconds) {
        // Clear existing timer if overwriting
        const existing = this.store.get(key);
        if (existing)
            clearTimeout(existing.timer);
        const timer = setTimeout(() => { this.store.delete(key); }, ttlSeconds * 1000);
        // Don't keep process alive just for cleanup timers
        if (timer.unref)
            timer.unref();
        this.store.set(key, { value, timer });
    }
    async get(key) {
        const entry = this.store.get(key);
        return entry ? entry.value : null;
    }
    async delete(key) {
        const entry = this.store.get(key);
        if (!entry)
            return false;
        clearTimeout(entry.timer);
        this.store.delete(key);
        return true;
    }
    async getAndDelete(key) {
        const entry = this.store.get(key);
        if (!entry)
            return null;
        clearTimeout(entry.timer);
        this.store.delete(key);
        return entry.value;
    }
}
exports.MemorySessionStore = MemorySessionStore;
// ---------------------------------------------------------------------------
// Redis implementation
// ---------------------------------------------------------------------------
class RedisSessionStore {
    client;
    prefix;
    constructor(client, prefix) {
        this.client = client;
        this.prefix = prefix;
    }
    async set(key, value, ttlSeconds) {
        await this.client.set(`${this.prefix}${key}`, value, { EX: ttlSeconds });
    }
    async get(key) {
        return this.client.get(`${this.prefix}${key}`);
    }
    async delete(key) {
        const count = await this.client.del(`${this.prefix}${key}`);
        return count > 0;
    }
    async getAndDelete(key) {
        return this.client.getDel(`${this.prefix}${key}`);
    }
}
exports.RedisSessionStore = RedisSessionStore;
