"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearBlockedWarnings = exports.getBlockedWarnings = exports.clearAllMemoryStores = exports.memoryLogger = exports.memoryConfigStore = exports.memoryAgentRegistry = exports.memorySessionStore = exports.memoryMessageQueue = exports.memoryKVStore = exports.isDBBlocked = exports.isMemoryMode = void 0;
exports.initializeNoPersistenceMode = initializeNoPersistenceMode;
exports.isPersistenceEnabled = isPersistenceEnabled;
const env_1 = require("../env");
const fs_blocker_1 = require("./fs-blocker");
Object.defineProperty(exports, "getBlockedWarnings", { enumerable: true, get: function () { return fs_blocker_1.getBlockedWarnings; } });
Object.defineProperty(exports, "clearBlockedWarnings", { enumerable: true, get: function () { return fs_blocker_1.clearBlockedWarnings; } });
const db_blocker_1 = require("./db-blocker");
Object.defineProperty(exports, "isDBBlocked", { enumerable: true, get: function () { return db_blocker_1.isDBBlocked; } });
const memory_store_1 = require("./memory-store");
Object.defineProperty(exports, "memoryKVStore", { enumerable: true, get: function () { return memory_store_1.memoryKVStore; } });
Object.defineProperty(exports, "memoryMessageQueue", { enumerable: true, get: function () { return memory_store_1.memoryMessageQueue; } });
Object.defineProperty(exports, "memorySessionStore", { enumerable: true, get: function () { return memory_store_1.memorySessionStore; } });
Object.defineProperty(exports, "memoryAgentRegistry", { enumerable: true, get: function () { return memory_store_1.memoryAgentRegistry; } });
Object.defineProperty(exports, "memoryConfigStore", { enumerable: true, get: function () { return memory_store_1.memoryConfigStore; } });
Object.defineProperty(exports, "clearAllMemoryStores", { enumerable: true, get: function () { return memory_store_1.clearAllMemoryStores; } });
const memory_logger_1 = require("./memory-logger");
Object.defineProperty(exports, "memoryLogger", { enumerable: true, get: function () { return memory_logger_1.memoryLogger; } });
const isMemoryMode = env_1.features.persistence.isMemoryMode;
exports.isMemoryMode = isMemoryMode;
function initializeNoPersistenceMode(projectRoot) {
    if (!isMemoryMode) {
        return;
    }
    console.warn('[Gateway] Running in NO-PERSISTENCE / MEMORY mode. All state is ephemeral.');
    (0, fs_blocker_1.initializeFSBlocker)(true, projectRoot);
    (0, fs_blocker_1.patchFSModule)();
    (0, db_blocker_1.initializeDBBlocker)(true);
    (0, memory_store_1.clearAllMemoryStores)();
    generateEphemeralSecrets();
    setupCleanupOnExit();
}
function generateEphemeralSecrets() {
    if (!env_1.env.JWT_SECRET) {
        const ephemeralJWT = generateRandomString(64);
        memory_store_1.memoryConfigStore.set('JWT_SECRET', ephemeralJWT);
        console.warn('[No-Persistence] Generated ephemeral JWT_SECRET (will not persist)');
    }
    if (!env_1.env.ENCRYPTION_KEY) {
        const ephemeralEncryption = generateRandomString(64);
        memory_store_1.memoryConfigStore.set('ENCRYPTION_KEY', ephemeralEncryption);
        console.warn('[No-Persistence] Generated ephemeral ENCRYPTION_KEY (will not persist)');
    }
}
function generateRandomString(length) {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    for (let i = 0; i < length; i++) {
        result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
}
function setupCleanupOnExit() {
    const cleanup = () => {
        console.warn('[No-Persistence] Cleaning up ephemeral state on exit...');
        (0, memory_store_1.clearAllMemoryStores)();
        (0, fs_blocker_1.clearBlockedWarnings)();
    };
    process.on('SIGTERM', cleanup);
    process.on('SIGINT', cleanup);
    process.on('exit', cleanup);
}
function isPersistenceEnabled() {
    return !isMemoryMode;
}
//# sourceMappingURL=index.js.map