/**
 * Load VisionClaw JSONL log files from a directory, parse them into LogEntry
 * objects, and return them sorted by timestamp.
 */
import fs from "node:fs";
import path from "node:path";
import readline from "node:readline";
export async function loadAllEntries(logsDir) {
    const files = fs
        .readdirSync(logsDir)
        .filter((f) => f.endsWith(".jsonl"))
        .sort()
        .map((f) => path.join(logsDir, f));
    if (files.length === 0) {
        console.error(`No .jsonl files found in ${logsDir}`);
        process.exit(1);
    }
    const entries = [];
    let parseErrors = 0;
    for (const filePath of files) {
        const fileName = path.basename(filePath);
        let lineIdx = 0;
        const rl = readline.createInterface({
            input: fs.createReadStream(filePath, { encoding: "utf-8" }),
            crlfDelay: Infinity,
        });
        for await (const raw of rl) {
            const trimmed = raw.trim();
            if (!trimmed) {
                lineIdx++;
                continue;
            }
            try {
                const obj = JSON.parse(trimmed);
                entries.push({
                    timestamp: obj.timestamp ?? "",
                    level: obj.level ?? "",
                    category: obj.category ?? "",
                    message: obj.message ?? "",
                    data: obj.data ?? {},
                    sourceFile: fileName,
                    lineIndex: lineIdx,
                });
            }
            catch {
                parseErrors++;
            }
            lineIdx++;
        }
    }
    entries.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
    console.log(`Loaded ${entries.length} entries from ${files.length} files (${parseErrors} parse errors)`);
    return entries;
}
//# sourceMappingURL=loader.js.map