import fs from "node:fs";
import path from "node:path";
export class PrStatusStore {
    cache = new Map();
    storeDir;
    storeFile;
    constructor(stateDir) {
        this.storeDir = stateDir;
        this.storeFile = path.join(stateDir, "pr-status-cache.json");
        this.load();
    }
    load() {
        try {
            if (fs.existsSync(this.storeFile)) {
                const data = JSON.parse(fs.readFileSync(this.storeFile, "utf-8"));
                if (data && typeof data === "object" && !Array.isArray(data)) {
                    for (const [id, entry] of Object.entries(data)) {
                        if (entry && typeof entry === "object")
                            this.cache.set(id, entry);
                    }
                }
            }
        }
        catch (err) {
            console.error("[pr-status-store] Failed to load:", err);
        }
    }
    save() {
        try {
            if (!fs.existsSync(this.storeDir))
                fs.mkdirSync(this.storeDir, { recursive: true });
            fs.writeFileSync(this.storeFile, JSON.stringify(Object.fromEntries(this.cache), null, 2), "utf-8");
        }
        catch (err) {
            console.error("[pr-status-store] Failed to save:", err);
        }
    }
    get(goalId) {
        return this.cache.get(goalId);
    }
    set(goalId, data) {
        this.cache.set(goalId, data);
        this.save();
    }
    getAll() {
        return Object.fromEntries(this.cache);
    }
    remove(goalId) {
        if (this.cache.delete(goalId))
            this.save();
    }
}
//# sourceMappingURL=pr-status-store.js.map