import fs from "node:fs";
import path from "node:path";
import os from "node:os";
const STORE_DIR = path.join(os.homedir(), ".pi");
const STORE_FILE = path.join(STORE_DIR, "gateway-swarms.json");
export class SwarmStore {
    swarms = new Map();
    constructor() {
        this.load();
    }
    load() {
        try {
            if (fs.existsSync(STORE_FILE)) {
                const data = JSON.parse(fs.readFileSync(STORE_FILE, "utf-8"));
                if (Array.isArray(data)) {
                    for (const s of data) {
                        if (s.goalId)
                            this.swarms.set(s.goalId, s);
                    }
                }
            }
        }
        catch (err) {
            console.error("[swarm-store] Failed to load:", err);
        }
    }
    save() {
        try {
            if (!fs.existsSync(STORE_DIR))
                fs.mkdirSync(STORE_DIR, { recursive: true });
            fs.writeFileSync(STORE_FILE, JSON.stringify(Array.from(this.swarms.values()), null, 2), "utf-8");
        }
        catch (err) {
            console.error("[swarm-store] Failed to save:", err);
        }
    }
    put(entry) {
        this.swarms.set(entry.goalId, entry);
        this.save();
    }
    get(goalId) {
        return this.swarms.get(goalId);
    }
    remove(goalId) {
        this.swarms.delete(goalId);
        this.save();
    }
    getAll() {
        return Array.from(this.swarms.values());
    }
}
//# sourceMappingURL=swarm-store.js.map