"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PrintCompletionSocketIoTask", {
    enumerable: true,
    get: function() {
        return PrintCompletionSocketIoTask;
    }
});
const _eventconstants = require("../constants/event.constants");
const _octoprintwebsocketconstants = require("../services/octoprint/constants/octoprint-websocket.constants");
const _correlationtokenutil = require("../utils/correlation-token.util");
class PrintCompletionSocketIoTask {
    eventEmitter2;
    socketIoGateway;
    logger;
    printerEventsCache;
    printCompletionService;
    contextCache = {};
    constructor({ eventEmitter2, socketIoGateway, printCompletionService, printerEventsCache, loggerFactory }){
        this.eventEmitter2 = eventEmitter2;
        this.socketIoGateway = socketIoGateway;
        this.printerEventsCache = printerEventsCache;
        this.printCompletionService = printCompletionService;
        this.logger = loggerFactory(PrintCompletionSocketIoTask.name);
        let that = this;
        this.eventEmitter2.on((0, _eventconstants.octoPrintWebsocketEvent)("*"), async function(octoPrintEvent, data) {
            await that.handleMessage(this.event, octoPrintEvent, data);
        });
    }
    get contexts() {
        return this.contextCache;
    }
    async handleMessage(fdmEvent, data) {
        const printerId = data.printerId;
        if (!printerId) {
            this.logger.log(`Skipping print completion log for FDM event ${fdmEvent}`);
        }
        if (data.event !== "event") {
            return;
        }
        const completion = {
            status: data.payload.type,
            fileName: data.payload?.payload?.name,
            createdAt: Date.now(),
            completionLog: data.payload?.error,
            printerId,
            context: {
                correlationId: null
            }
        };
        if (data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.EStop || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.PrintCancelling || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.PrintCancelled || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Home || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.TransferStarted || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.TransferDone || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Disconnecting || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Disconnected || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.MetadataAnalysisStarted || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.MetadataAnalysisFinished || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Error) {
            this.contextCache[printerId] = {
                ...this.contextCache[printerId],
                [data.payload.type]: completion
            };
            if (data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Disconnecting || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.Disconnected) {
                await this.printerEventsCache.setSubstate(printerId, "current", "state", {
                    text: data.payload.type,
                    flags: {
                        operational: false,
                        printing: false,
                        ready: false,
                        closedOrError: true,
                        error: false
                    }
                });
            }
            const corrId = this.contextCache[printerId].correlationId;
            await this.printCompletionService.updateContext(corrId, this.contextCache[printerId]);
            return;
        }
        if (data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.PrintStarted) {
            const token = (0, _correlationtokenutil.generateCorrelationToken)();
            this.contextCache[printerId] = {
                correlationId: token
            };
            completion.context = this.contextCache[printerId];
            await this.printCompletionService.create(completion);
        } else if (data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.PrintFailed || data.payload.type === _octoprintwebsocketconstants.EVENT_TYPES.PrintDone) {
            completion.context = this.contextCache[printerId];
            await this.printCompletionService.create(completion);
            this.eventEmitter2.emit((0, _eventconstants.fdmMonsterPrinterStoppedEvent)(printerId));
            this.contextCache[printerId] = {};
        }
    }
    async run() {
        this.contextCache = await this.printCompletionService.loadPrintContexts();
    }
}

//# sourceMappingURL=print-completion.socketio.task.js.map