"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.rankMemories = rankMemories;
exports.getTopMemoriesForContext = getTopMemoriesForContext;
const database_1 = require("../database/database");
function rankMemories(opts = {}) {
    const db = (0, database_1.getDb)();
    const conditions = [];
    const params = [];
    if (opts.project) {
        conditions.push('project = ?');
        params.push(opts.project);
    }
    if (opts.minImportance !== undefined) {
        conditions.push('importance >= ?');
        params.push(opts.minImportance);
    }
    if (opts.maxAgeDays) {
        const since = new Date(Date.now() - opts.maxAgeDays * 86400000).toISOString();
        conditions.push('created_at >= ?');
        params.push(since);
    }
    if (opts.topic) {
        conditions.push('topic LIKE ?');
        params.push(`%${opts.topic}%`);
    }
    if (opts.query) {
        conditions.push('(summary LIKE ? OR topic LIKE ? OR details_json LIKE ? OR tags_json LIKE ?)');
        const q = `%${opts.query}%`;
        params.push(q, q, q, q);
    }
    const where = conditions.length > 0 ? 'WHERE ' + conditions.join(' AND ') : '';
    const limit = opts.maxResults || 20;
    const rows = db.prepare(`SELECT id, type, topic, summary, importance, created_at, files_json
     FROM memory_events ${where}
     ORDER BY importance DESC, created_at DESC
     LIMIT ?`).all(...params, limit);
    return rows.map(r => ({
        ...r,
        relevance: calculateRelevance(r, opts),
    })).sort((a, b) => b.relevance - a.relevance);
}
function calculateRelevance(row, opts) {
    let score = row.importance * 10;
    // Recency boost (newer = higher)
    const ageMs = Date.now() - new Date(row.created_at).getTime();
    const ageDays = ageMs / 86400000;
    if (ageDays < 1)
        score += 30;
    else if (ageDays < 7)
        score += 20;
    else if (ageDays < 30)
        score += 10;
    // Type boost
    const typeBoost = {
        decision: 25, error: 20, fix: 22, publish: 18, handoff: 15,
        pattern: 24, style: 20, risk: 19, question: 12, ai_work: 14,
        session: 10, branch: 8, edit: 5, verification: 8, file_event: 2,
    };
    score += typeBoost[row.type] || 0;
    // Topic match boost
    if (opts.topic && row.topic && row.topic.toLowerCase().includes(opts.topic.toLowerCase())) {
        score += 40;
    }
    // Query match boost
    if (opts.query && row.summary.toLowerCase().includes(opts.query.toLowerCase())) {
        score += 35;
    }
    return Math.round(score);
}
function getTopMemoriesForContext(project, maxWords = 2000) {
    const memories = rankMemories({ project, maxResults: 50, minImportance: 3 });
    const result = [];
    let wordCount = 0;
    for (const mem of memories) {
        const summaryWords = mem.summary.split(/\s+/).length;
        if (wordCount + summaryWords > maxWords)
            break;
        result.push(mem);
        wordCount += summaryWords;
    }
    return result;
}
//# sourceMappingURL=context-ranker.js.map