"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.OutcomeStore = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class OutcomeStore {
    events;
    summaries;
    storePath;
    constructor(cellDir) {
        this.events = new Map();
        this.summaries = new Map();
        this.storePath = path.join(cellDir, 'outcomes.json');
        this.ensureDir(cellDir);
        this.load();
    }
    addEvent(event) {
        this.events.set(event.id, event);
    }
    getEvent(id) {
        return this.events.get(id);
    }
    getEvents(patternId) {
        const result = [];
        for (const evt of this.events.values()) {
            if (evt.patternId === patternId)
                result.push(evt);
        }
        return result.sort((a, b) => a.timestamp - b.timestamp);
    }
    getAllEvents() {
        return Array.from(this.events.values()).sort((a, b) => a.timestamp - b.timestamp);
    }
    getEventCount() {
        return this.events.size;
    }
    addSummary(summary) {
        this.summaries.set(summary.patternId, summary);
    }
    getSummary(patternId) {
        return this.summaries.get(patternId);
    }
    getAllSummaries() {
        return Array.from(this.summaries.values());
    }
    getSummaryCount() {
        return this.summaries.size;
    }
    save() {
        const storeData = {
            events: Array.from(this.events.values()),
            summaries: Object.fromEntries(this.summaries),
        };
        fs.writeFileSync(this.storePath, JSON.stringify(storeData, null, 2), 'utf-8');
    }
    clear() {
        this.events.clear();
        this.summaries.clear();
        this.save();
    }
    load() {
        if (!fs.existsSync(this.storePath))
            return;
        try {
            const raw = fs.readFileSync(this.storePath, 'utf-8');
            const data = JSON.parse(raw);
            if (data.events)
                for (const evt of data.events)
                    this.events.set(evt.id, evt);
            if (data.summaries) {
                for (const [key, val] of Object.entries(data.summaries)) {
                    this.summaries.set(key, val);
                }
            }
        }
        catch {
            this.events.clear();
            this.summaries.clear();
        }
    }
    ensureDir(dir) {
        if (!fs.existsSync(dir))
            fs.mkdirSync(dir, { recursive: true });
    }
}
exports.OutcomeStore = OutcomeStore;
//# sourceMappingURL=outcome-store.js.map