"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.memoryConfigStore = exports.memoryAgentRegistry = exports.memorySessionStore = exports.memoryMessageQueue = exports.memoryKVStore = exports.MemoryConfigStore = exports.MemoryAgentRegistry = exports.MemorySessionStore = exports.MemoryMessageQueue = exports.MemoryKVStore = void 0;
exports.clearAllMemoryStores = clearAllMemoryStores;
class MemoryKVStore {
    constructor() {
        this.store = new Map();
    }
    get(key) {
        const item = this.store.get(key);
        if (!item)
            return null;
        if (item.expiresAt && Date.now() > item.expiresAt) {
            this.store.delete(key);
            return null;
        }
        return item.value;
    }
    set(key, value, ttlMs) {
        if (ttlMs) {
            const expiresAt = Date.now() + ttlMs;
            this.store.set(key, { value, expiresAt });
        }
        else {
            this.store.set(key, { value });
        }
    }
    delete(key) {
        return this.store.delete(key);
    }
    has(key) {
        const item = this.store.get(key);
        if (!item)
            return false;
        if (item.expiresAt && Date.now() > item.expiresAt) {
            this.store.delete(key);
            return false;
        }
        return true;
    }
    clear() {
        this.store.clear();
    }
    keys() {
        const now = Date.now();
        const validKeys = [];
        for (const [key, item] of this.store.entries()) {
            if (!item.expiresAt || item.expiresAt > now) {
                validKeys.push(key);
            }
            else {
                this.store.delete(key);
            }
        }
        return validKeys;
    }
    size() {
        const now = Date.now();
        let count = 0;
        for (const [key, item] of this.store.entries()) {
            if (item.expiresAt && item.expiresAt <= now) {
                this.store.delete(key);
            }
            else {
                count++;
            }
        }
        return count;
    }
}
exports.MemoryKVStore = MemoryKVStore;
class MemoryMessageQueue {
    constructor() {
        this.queues = new Map();
    }
    enqueue(queueName, data) {
        if (!this.queues.has(queueName)) {
            this.queues.set(queueName, []);
        }
        const id = `msg-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
        const queue = this.queues.get(queueName);
        queue.push({ id, data, timestamp: Date.now() });
        return id;
    }
    dequeue(queueName) {
        const queue = this.queues.get(queueName);
        if (!queue || queue.length === 0)
            return null;
        const item = queue.shift();
        if (!item)
            return null;
        return { id: item.id, data: item.data };
    }
    peek(queueName) {
        const queue = this.queues.get(queueName);
        if (!queue || queue.length === 0)
            return null;
        const item = queue[0];
        if (!item)
            return null;
        return { id: item.id, data: item.data };
    }
    size(queueName) {
        return this.queues.get(queueName)?.length || 0;
    }
    clear(queueName) {
        if (queueName) {
            this.queues.delete(queueName);
        }
        else {
            this.queues.clear();
        }
    }
}
exports.MemoryMessageQueue = MemoryMessageQueue;
class MemorySessionStore {
    constructor() {
        this.sessions = new Map();
    }
    get(sessionId) {
        const session = this.sessions.get(sessionId);
        if (!session)
            return null;
        if (session.expiresAt && Date.now() > session.expiresAt) {
            this.sessions.delete(sessionId);
            return null;
        }
        return session.data;
    }
    set(sessionId, data, ttlMs) {
        if (ttlMs) {
            const expiresAt = Date.now() + ttlMs;
            this.sessions.set(sessionId, { data, expiresAt });
        }
        else {
            this.sessions.set(sessionId, { data });
        }
    }
    delete(sessionId) {
        return this.sessions.delete(sessionId);
    }
    has(sessionId) {
        const session = this.sessions.get(sessionId);
        if (!session)
            return false;
        if (session.expiresAt && Date.now() > session.expiresAt) {
            this.sessions.delete(sessionId);
            return false;
        }
        return true;
    }
    clear() {
        this.sessions.clear();
    }
}
exports.MemorySessionStore = MemorySessionStore;
class MemoryAgentRegistry {
    constructor() {
        this.agents = new Map();
    }
    register(agentId, agentData) {
        this.agents.set(agentId, { ...agentData, registeredAt: Date.now() });
    }
    get(agentId) {
        return this.agents.get(agentId) || null;
    }
    unregister(agentId) {
        return this.agents.delete(agentId);
    }
    list() {
        return Array.from(this.agents.values());
    }
    has(agentId) {
        return this.agents.has(agentId);
    }
    clear() {
        this.agents.clear();
    }
}
exports.MemoryAgentRegistry = MemoryAgentRegistry;
class MemoryConfigStore {
    constructor() {
        this.config = new Map();
    }
    get(key) {
        return this.config.get(key) || null;
    }
    set(key, value) {
        this.config.set(key, value);
    }
    delete(key) {
        return this.config.delete(key);
    }
    has(key) {
        return this.config.has(key);
    }
    getAll() {
        const result = {};
        for (const [key, value] of this.config.entries()) {
            result[key] = value;
        }
        return result;
    }
    clear() {
        this.config.clear();
    }
}
exports.MemoryConfigStore = MemoryConfigStore;
exports.memoryKVStore = new MemoryKVStore();
exports.memoryMessageQueue = new MemoryMessageQueue();
exports.memorySessionStore = new MemorySessionStore();
exports.memoryAgentRegistry = new MemoryAgentRegistry();
exports.memoryConfigStore = new MemoryConfigStore();
function clearAllMemoryStores() {
    exports.memoryKVStore.clear();
    exports.memoryMessageQueue.clear();
    exports.memorySessionStore.clear();
    exports.memoryAgentRegistry.clear();
    exports.memoryConfigStore.clear();
}
//# sourceMappingURL=memory-store.js.map