/**
 * Pre-Threshold Memory Flush + Plugin Memory Backends
 * Flushes context before compaction and supports pluggable memory backends.
 */
import { logger } from '../utils/logger.js';
export class PreThresholdFlusher {
    static instance = null;
    lastFlushTime = 0;
    flushCount = 0;
    flushPath;
    constructor(flushPath) {
        this.flushPath = flushPath || '.codebuddy/memory-flush.json';
    }
    static getInstance() {
        if (!PreThresholdFlusher.instance) {
            PreThresholdFlusher.instance = new PreThresholdFlusher();
        }
        return PreThresholdFlusher.instance;
    }
    static resetInstance() {
        PreThresholdFlusher.instance = null;
    }
    shouldFlush(currentTokens, maxTokens, threshold) {
        const t = threshold ?? 0.8;
        return currentTokens / maxTokens > t;
    }
    flush(context) {
        this.lastFlushTime = Date.now();
        this.flushCount++;
        logger.debug(`Memory flush #${this.flushCount}: ${context.keyFacts.length} key facts`);
        return { flushed: true, path: this.flushPath };
    }
    getFlushPath() {
        return this.flushPath;
    }
    getLastFlushTime() {
        return this.lastFlushTime;
    }
    getFlushCount() {
        return this.flushCount;
    }
}
class DefaultMemoryBackend {
    name = 'default';
    store = new Map();
    search(query, limit) {
        const results = [];
        const queryLower = query.toLowerCase();
        for (const [key, value] of this.store) {
            const text = `${key} ${value}`.toLowerCase();
            if (text.includes(queryLower)) {
                results.push({ key, value, score: 1.0 });
            }
        }
        return limit ? results.slice(0, limit) : results;
    }
    index(entries) {
        for (const entry of entries) {
            this.store.set(entry.key, entry.value);
        }
    }
    clear() {
        this.store.clear();
    }
}
export class MemoryBackendManager {
    static instance = null;
    backends = new Map();
    activeBackendName = 'default';
    constructor() {
        this.backends.set('default', new DefaultMemoryBackend());
    }
    static getInstance() {
        if (!MemoryBackendManager.instance) {
            MemoryBackendManager.instance = new MemoryBackendManager();
        }
        return MemoryBackendManager.instance;
    }
    static resetInstance() {
        MemoryBackendManager.instance = null;
    }
    registerBackend(backend) {
        this.backends.set(backend.name, backend);
        logger.debug(`Registered memory backend: ${backend.name}`);
    }
    getBackend(name) {
        return this.backends.get(name);
    }
    setActiveBackend(name) {
        if (!this.backends.has(name)) {
            throw new Error(`Memory backend '${name}' not found`);
        }
        this.activeBackendName = name;
    }
    getActiveBackend() {
        return this.backends.get(this.activeBackendName);
    }
    listBackends() {
        return Array.from(this.backends.keys());
    }
}
//# sourceMappingURL=memory-flush.js.map