import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
export class LearningReviewStore {
    dir;
    constructor(options) {
        this.dir = join(options.agentDir, "learning", "reviews");
    }
    list() {
        if (!existsSync(this.dir))
            return [];
        return readdirSync(this.dir)
            .filter((entry) => entry.endsWith(".json"))
            .map((entry) => this.read(entry.replace(/\.json$/u, "")))
            .filter((record) => record !== undefined)
            .sort((a, b) => b.createdAt.localeCompare(a.createdAt));
    }
    read(id) {
        try {
            return JSON.parse(readFileSync(this.pathFor(id), "utf-8"));
        }
        catch {
            return undefined;
        }
    }
    write(record) {
        mkdirSync(this.dir, { recursive: true });
        writeFileSync(this.pathFor(record.id), `${JSON.stringify(record, null, 2)}\n`, "utf-8");
        return record;
    }
    update(id, update) {
        const record = this.read(id);
        if (!record)
            throw new Error(`Learning review not found: ${id}`);
        return this.write(update(record));
    }
    pathFor(id) {
        return join(this.dir, `${id}.json`);
    }
}
export function createReviewId(date = new Date()) {
    const stamp = date.toISOString().replace(/[:.]/g, "-");
    const suffix = Math.random().toString(36).slice(2, 8);
    return `review-${stamp}-${suffix}`;
}
//# sourceMappingURL=review-store.js.map