"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.compactMemoryEvents = compactMemoryEvents;
exports.compactArchiveChunks = compactArchiveChunks;
exports.getMemoryHealth = getMemoryHealth;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const database_1 = require("../database/database");
function compactMemoryEvents(_olderThanDays = 90) {
    return 0;
}
function compactArchiveChunks(projectDir, olderThanDays = 90) {
    const archiveDir = path.join(projectDir, '.cell', 'memory', 'archive');
    if (!fs.existsSync(archiveDir)) {
        return { mergedChunks: 0, deletedChunks: 0, keptChunks: 0, totalRecords: 0 };
    }
    let mergedChunks = 0;
    let deletedChunks = 0;
    let keptChunks = 0;
    let totalRecords = 0;
    function walk(dir) {
        if (!fs.existsSync(dir))
            return;
        for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
            const full = path.join(dir, entry.name);
            if (entry.isDirectory()) {
                walk(full);
                continue;
            }
            if (entry.name.endsWith('.meta.json')) {
                try {
                    const meta = JSON.parse(fs.readFileSync(full, 'utf-8'));
                    keptChunks++;
                    totalRecords += meta.records || 0;
                }
                catch { /* skip corrupted */ }
            }
        }
    }
    walk(archiveDir);
    return { mergedChunks, deletedChunks, keptChunks, totalRecords };
}
function getMemoryHealth(projectDir) {
    const db = (0, database_1.getDb)();
    const totalRow = db.prepare('SELECT COUNT(*) as c FROM memory_events').get();
    const oldestRow = db.prepare('SELECT MIN(created_at) as o FROM memory_events').get();
    const newestRow = db.prepare('SELECT MAX(created_at) as n FROM memory_events').get();
    const avgRow = db.prepare('SELECT AVG(importance) as a FROM memory_events').get();
    const archiveDir = path.join(projectDir, '.cell', 'memory', 'archive');
    let archiveChunks = 0;
    if (fs.existsSync(archiveDir)) {
        function countChunks(dir) {
            if (!fs.existsSync(dir))
                return;
            for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
                const full = path.join(dir, entry.name);
                if (entry.isDirectory()) {
                    countChunks(full);
                    continue;
                }
                if (entry.name.endsWith('.meta.json'))
                    archiveChunks++;
            }
        }
        countChunks(archiveDir);
    }
    const currentPath = path.join(projectDir, '.cell', 'handoff', 'current.md');
    let handoffLines = 0;
    if (fs.existsSync(currentPath)) {
        handoffLines = fs.readFileSync(currentPath, 'utf-8').split('\n').length;
    }
    return {
        eventCount: totalRow.c,
        archiveChunks,
        handoffLines,
        oldestEvent: oldestRow.o,
        newestEvent: newestRow.n,
        avgImportance: Math.round((avgRow.a || 0) * 10) / 10,
    };
}
//# sourceMappingURL=memory-compact.js.map