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