import fs from "node:fs";
import path from "node:path";
const STAFF_ACCESSORY_IDS = new Set([
    "none",
    "crown",
    "bandana",
    "magnifier",
    "palette",
    "pencil",
    "shield",
    "set-square",
    "flask",
    "wizard-hat",
    "wand",
    "stamp",
    "clipboard",
]);
export function normalizeStaffAccessory(value) {
    if (typeof value !== "string")
        return "none";
    const id = value.trim();
    return STAFF_ACCESSORY_IDS.has(id) ? id : "none";
}
function normalizeStaffRecord(staff) {
    // Legacy records lack `sandboxed`; normalise to false.
    staff.sandboxed = !!staff.sandboxed;
    // Legacy records lack `contextPolicy`; normalise to "compact".
    if (staff.contextPolicy !== "preserve" && staff.contextPolicy !== "compact") {
        staff.contextPolicy = "compact";
    }
    // Legacy/malformed records lack a valid accessory; normalise to "none".
    staff.accessory = normalizeStaffAccessory(staff.accessory);
    return staff;
}
/**
 * Simple JSON file store for staff agents.
 * Staff persist across server restarts.
 */
export class StaffStore {
    storeDir;
    storeFile;
    staff = new Map();
    constructor(stateDir) {
        this.storeDir = stateDir;
        this.storeFile = path.join(stateDir, "staff.json");
        this.load();
    }
    load() {
        try {
            if (fs.existsSync(this.storeFile)) {
                const data = JSON.parse(fs.readFileSync(this.storeFile, "utf-8"));
                if (Array.isArray(data)) {
                    for (const s of data) {
                        if (s.id) {
                            this.staff.set(s.id, normalizeStaffRecord(s));
                        }
                    }
                }
            }
        }
        catch (err) {
            console.error("[staff-store] Failed to load persisted staff:", err);
        }
    }
    save() {
        try {
            if (!fs.existsSync(this.storeDir)) {
                fs.mkdirSync(this.storeDir, { recursive: true });
            }
            const data = Array.from(this.staff.values());
            fs.writeFileSync(this.storeFile, JSON.stringify(data, null, 2), "utf-8");
        }
        catch (err) {
            console.error("[staff-store] Failed to save staff:", err);
        }
    }
    put(staff) {
        // Normalise on every write so the in-memory record always carries
        // real values. Mirrors the load-side normalisation.
        this.staff.set(staff.id, normalizeStaffRecord(staff));
        this.save();
    }
    get(id) {
        return this.staff.get(id);
    }
    remove(id) {
        this.staff.delete(id);
        this.save();
    }
    getAll() {
        return Array.from(this.staff.values());
    }
    update(id, updates) {
        const existing = this.staff.get(id);
        if (!existing)
            return false;
        // Strip undefined values to avoid overwriting existing fields.
        // null is treated as "clear this field" (delete the key).
        const rec = existing;
        for (const [k, v] of Object.entries(updates)) {
            if (v === undefined)
                continue;
            if (v === null) {
                delete rec[k];
            }
            else {
                rec[k] = v;
            }
        }
        existing.updatedAt = Date.now();
        normalizeStaffRecord(existing);
        this.save();
        return true;
    }
}
//# sourceMappingURL=staff-store.js.map