"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PrintCompletionService", {
    enumerable: true,
    get: function() {
        return PrintCompletionService;
    }
});
const _baseservice = require("./base.service");
const _entities = require("../../entities");
const _printcompletiondto = require("../interfaces/print-completion.dto");
const _typeorm = require("typeorm");
const _octoprintwebsocketconstants = require("../octoprint/constants/octoprint-websocket.constants");
const _arrayutil = require("../../utils/array.util");
const _printcompletionshared = require("../mongoose/print-completion.shared");
class PrintCompletionService extends (0, _baseservice.BaseService)(_entities.PrintCompletion, _printcompletiondto.PrintCompletionDto) {
    logger;
    constructor({ typeormService, loggerFactory }){
        super({
            typeormService
        });
        this.logger = loggerFactory(PrintCompletionService.name);
    }
    toDto(entity) {
        return {
            id: entity.id,
            completionLog: entity.completionLog,
            context: entity.context,
            fileName: entity.fileName,
            createdAt: entity.createdAt,
            printerReference: entity.printerReference,
            status: entity.status,
            printerId: entity.printerId
        };
    }
    async create(input) {
        return await super.create(input);
    }
    async findPrintCompletion(correlationId) {
        const completions = await this.repository.findBy({});
        return completions.filter((c)=>c.context?.correlationId === correlationId);
    }
    async updateContext(correlationId, context) {
        if (!correlationId?.length) {
            this.logger.warn("Ignoring undefined correlationId, cant update print completion context");
            return;
        }
        const completionEntry = await this.repository.findOneBy({
            context: {
                correlationId
            },
            status: _octoprintwebsocketconstants.EVENT_TYPES.PrintStarted
        });
        if (!completionEntry) {
            this.logger.warn(`Print with correlationId ${correlationId} could not be updated with new context as it was not found`);
            return;
        }
        completionEntry.context = context;
        await this.update(completionEntry.id, completionEntry);
    }
    async listGroupByPrinterStatus() {
        const limitedCompletions = await this.listPaged();
        const printCompletionsAggr = (0, _arrayutil.groupArrayBy)(limitedCompletions, (val)=>val.printerId.toString());
        const completions = Object.entries(printCompletionsAggr).map(([pc, val])=>({
                printerId: parseInt(pc),
                printEvents: val
            }));
        return (0, _printcompletionshared.processCompletions)(completions);
    }
    async loadPrintContexts() {
        const completions = await this.repository.find({
            where: {
                status: (0, _typeorm.Not)((0, _typeorm.In)([
                    _octoprintwebsocketconstants.EVENT_TYPES.PrintDone,
                    _octoprintwebsocketconstants.EVENT_TYPES.PrintFailed
                ]))
            },
            order: {
                printerId: 1,
                createdAt: -1
            }
        });
        return (0, _arrayutil.groupArrayBy)(completions, (val)=>val.id.toString());
    }
}

//# sourceMappingURL=print-completion.service.js.map