"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PrinterEventsCache", {
    enumerable: true,
    get: function() {
        return PrinterEventsCache;
    }
});
const _keydiffcache = require("../utils/cache/key-diff.cache");
const _metricutils = require("../utils/metric.utils");
const _eventconstants = require("../constants/event.constants");
class PrinterEventsCache extends _keydiffcache.KeyDiffCache {
    logger;
    eventEmitter2;
    settingsStore;
    constructor({ eventEmitter2, loggerFactory, settingsStore }){
        super();
        this.settingsStore = settingsStore;
        this.logger = loggerFactory(PrinterEventsCache.name);
        this.eventEmitter2 = eventEmitter2;
        this.subscribeToEvents();
    }
    get _debugMode() {
        return this.settingsStore.getSettingsSensitive()?.server?.debugSettings?.debugSocketMessages;
    }
    async deletePrinterSocketEvents(id) {
        await this.deleteKeyValue(id, true);
    }
    async getPrinterSocketEvents(id) {
        return this.keyValueStore[id];
    }
    async getOrCreateEvents(printerId) {
        let ref = await this.getValue(printerId);
        if (!ref) {
            ref = {
                connected: null,
                reauthRequired: null,
                notify_status_update: null,
                current: null,
                history: null,
                API_STATE_UPDATED: null,
                WS_CLOSED: null,
                WS_ERROR: null,
                WS_OPENED: null,
                WS_STATE_UPDATED: null
            };
            await this.setKeyValue(printerId, ref);
        }
        return ref;
    }
    async setEvent(printerId, label, payload) {
        const ref = await this.getOrCreateEvents(printerId);
        ref[label] = {
            payload,
            receivedAt: Date.now()
        };
        await this.setKeyValue(printerId, ref);
    }
    async setSubstate(printerId, label, substateName, payload) {
        const ref = await this.getOrCreateEvents(printerId);
        if (!ref[label]) {
            ref[label] = {};
        }
        ref[label][substateName] = {
            payload,
            receivedAt: Date.now()
        };
        await this.setKeyValue(printerId, ref);
    }
    async handlePrintersDeleted({ printerIds }) {
        await this.deleteKeysBatch(printerIds);
    }
    subscribeToEvents() {
        this.eventEmitter2.on("octoprint.*", (e)=>this.onOctoPrintSocketMessage(e));
        this.eventEmitter2.on("moonraker.*", (e)=>this.onMoonrakerSocketMessage(e));
        this.eventEmitter2.on(_eventconstants.printerEvents.printersDeleted, this.handlePrintersDeleted.bind(this));
    }
    async onOctoPrintSocketMessage(e) {
        const printerId = e.printerId;
        if (![
            "plugin",
            "event"
        ].includes(e.event)) {
            await this.setEvent(printerId, e.event, e.event === "history" ? this.pruneHistoryPayload(e.payload) : e.payload);
            if (this._debugMode) {
                this.logger.log(`Message '${e.event}' received, size ${(0, _metricutils.formatKB)(e.payload)}`, e.printerId);
            }
        }
    }
    async onMoonrakerSocketMessage(e) {
        const printerId = e.printerId;
        const eventType = e.event;
        if ([
            "notify_status_update",
            "current"
        ].includes(eventType)) {
            await this.setEvent(printerId, eventType, e.payload);
        }
    }
    pruneHistoryPayload(payload) {
        delete payload.logs;
        delete payload.temps;
        delete payload.messages;
        return payload;
    }
}

//# sourceMappingURL=printer-events.cache.js.map