"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryRetentionPolicy = void 0;
class MemoryRetentionPolicy {
    decide(record) {
        // v1.11.0 — explicit `keep: true` flag bypasses type-based expiry. Used
        // for cross-process facts that must outlive default retention windows.
        if (record.keep === true) {
            return {
                keep: true,
                rationale: 'Record marked keep:true — eternal retention regardless of type.'
            };
        }
        const createdAt = new Date(record.createdAt).getTime();
        const now = Date.now();
        const ageDays = (now - createdAt) / (1000 * 60 * 60 * 24);
        if (record.type === 'working') {
            return {
                keep: ageDays <= 7,
                expiresAt: new Date(createdAt + 7 * 24 * 60 * 60 * 1000).toISOString(),
                rationale: 'Working memory expira rápido para evitar entulho operacional.'
            };
        }
        if (record.type === 'temporal') {
            return {
                keep: ageDays <= 30,
                expiresAt: new Date(createdAt + 30 * 24 * 60 * 60 * 1000).toISOString(),
                rationale: 'Temporal memory retida por janela curta de contexto.'
            };
        }
        if (record.type === 'profile') {
            return {
                keep: true,
                rationale: 'Profile memory é persistente por padrão e exige remoção explícita.'
            };
        }
        return {
            keep: true,
            rationale: 'Episodic/semantic memory pode permanecer até futura curadoria.'
        };
    }
    score(record, query) {
        const terms = query.toLowerCase().split(/\s+/).filter(Boolean);
        const hay = `${record.content} ${record.summary || ''} ${(record.tags || []).join(' ')}`.toLowerCase();
        const lexical = terms.reduce((acc, term) => acc + (hay.includes(term) ? 1 : 0), 0);
        const confidence = record.confidence || 0.5;
        const recencyBoost = this.recencyBoost(record.type, record.createdAt);
        const typeBoost = this.typeBoost(record.type);
        return lexical * 3 + confidence * 2 + recencyBoost + typeBoost;
    }
    recencyBoost(type, createdAt) {
        const ageHours = (Date.now() - new Date(createdAt).getTime()) / (1000 * 60 * 60);
        if (type === 'working')
            return ageHours < 24 ? 3 : ageHours < 72 ? 2 : 0;
        if (type === 'temporal')
            return ageHours < 168 ? 2 : 0;
        return ageHours < 168 ? 1 : 0;
    }
    typeBoost(type) {
        switch (type) {
            case 'profile': return 3;
            case 'semantic': return 2;
            case 'episodic': return 1;
            default: return 0;
        }
    }
}
exports.MemoryRetentionPolicy = MemoryRetentionPolicy;
