import fs from "node:fs";
import path from "node:path";
/**
 * Server-side store for review annotations. One JSON file per session.
 * Persisted to `.bobbit/state/review-annotations-{sessionId}.json`.
 */
export class ReviewAnnotationStore {
    stateDir;
    constructor(stateDir) {
        this.stateDir = stateDir;
    }
    filePath(sessionId) {
        return path.join(this.stateDir, `review-annotations-${sessionId}.json`);
    }
    read(sessionId) {
        try {
            const fp = this.filePath(sessionId);
            if (fs.existsSync(fp)) {
                const raw = JSON.parse(fs.readFileSync(fp, "utf-8"));
                if (raw && typeof raw === "object" && !Array.isArray(raw)) {
                    return {
                        annotations: raw.annotations ?? {},
                        submitted: !!raw.submitted,
                    };
                }
            }
        }
        catch (err) {
            console.error("[review-annotation-store] Failed to read:", err);
        }
        return { annotations: {}, submitted: false };
    }
    write(sessionId, data) {
        try {
            if (!fs.existsSync(this.stateDir))
                fs.mkdirSync(this.stateDir, { recursive: true });
            fs.writeFileSync(this.filePath(sessionId), JSON.stringify(data, null, 2), "utf-8");
        }
        catch (err) {
            console.error("[review-annotation-store] Failed to write:", err);
        }
    }
    getAll(sessionId) {
        return this.read(sessionId);
    }
    addAnnotation(sessionId, docTitle, annotation) {
        const data = this.read(sessionId);
        if (!data.annotations[docTitle]) {
            data.annotations[docTitle] = [];
        }
        // Replace if same id exists (upsert), otherwise append
        const idx = data.annotations[docTitle].findIndex((a) => a.id === annotation.id);
        if (idx >= 0) {
            data.annotations[docTitle][idx] = annotation;
        }
        else {
            data.annotations[docTitle].push(annotation);
        }
        this.write(sessionId, data);
    }
    removeAnnotation(sessionId, docTitle, annotationId) {
        const data = this.read(sessionId);
        if (data.annotations[docTitle]) {
            data.annotations[docTitle] = data.annotations[docTitle].filter((a) => a.id !== annotationId);
            if (data.annotations[docTitle].length === 0) {
                delete data.annotations[docTitle];
            }
            this.write(sessionId, data);
        }
    }
    clearAnnotations(sessionId, docTitle) {
        const data = this.read(sessionId);
        if (data.annotations[docTitle]) {
            delete data.annotations[docTitle];
            this.write(sessionId, data);
        }
    }
    clearAll(sessionId) {
        const data = this.read(sessionId);
        data.annotations = {};
        this.write(sessionId, data);
    }
    setSubmitted(sessionId, value) {
        const data = this.read(sessionId);
        data.submitted = value;
        this.write(sessionId, data);
    }
    isSubmitted(sessionId) {
        return this.read(sessionId).submitted;
    }
    /**
     * Overwrite the entire state for a session (used by bulk save / sendBeacon).
     */
    writeAll(sessionId, annotations, submitted) {
        this.write(sessionId, { annotations, submitted });
    }
    deleteFile(sessionId) {
        try {
            const fp = this.filePath(sessionId);
            if (fs.existsSync(fp)) {
                fs.unlinkSync(fp);
            }
        }
        catch (err) {
            console.error("[review-annotation-store] Failed to delete:", err);
        }
    }
}
//# sourceMappingURL=review-annotation-store.js.map