import fs from "node:fs";
import path from "node:path";
/**
 * Simple key-value store persisted to .bobbit/state/preferences.json.
 * Auto-saves on every set/remove. Handles missing file gracefully.
 */
export class PreferencesStore {
    data = {};
    storeFile;
    constructor(stateDir) {
        this.storeFile = path.join(stateDir, "preferences.json");
        this.load();
    }
    load() {
        try {
            if (fs.existsSync(this.storeFile)) {
                const raw = JSON.parse(fs.readFileSync(this.storeFile, "utf-8"));
                if (raw && typeof raw === "object" && !Array.isArray(raw)) {
                    this.data = raw;
                }
            }
        }
        catch (err) {
            console.error("[preferences-store] Failed to load preferences:", err);
        }
    }
    save() {
        try {
            const dir = path.dirname(this.storeFile);
            if (!fs.existsSync(dir)) {
                fs.mkdirSync(dir, { recursive: true });
            }
            fs.writeFileSync(this.storeFile, JSON.stringify(this.data, null, 2), "utf-8");
        }
        catch (err) {
            console.error("[preferences-store] Failed to save preferences:", err);
        }
    }
    get(key) {
        return this.data[key];
    }
    set(key, value) {
        this.data[key] = value;
        this.save();
    }
    getAll() {
        return { ...this.data };
    }
    remove(key) {
        delete this.data[key];
        this.save();
    }
}
//# sourceMappingURL=preferences-store.js.map