"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PrinterService", {
    enumerable: true,
    get: function() {
        return PrinterService;
    }
});
const _models = require("../../models");
const _runtimeexceptions = require("../../exceptions/runtime.exceptions");
const _validators = require("../../handlers/validators");
const _printerservicevalidation = require("../validators/printer-service.validation");
const _eventconstants = require("../../constants/event.constants");
const _urlutils = require("../../utils/url.utils");
class PrinterService {
    eventEmitter2;
    logger;
    constructor({ eventEmitter2, loggerFactory }){
        this.eventEmitter2 = eventEmitter2;
        this.logger = loggerFactory(PrinterService.name);
    }
    toUnsafeDto(entity) {
        return {
            ...this.toDto(entity),
            apiKey: entity.apiKey,
            printerURL: entity.printerURL,
            printerType: entity.printerType
        };
    }
    toDto(entity) {
        return {
            id: entity.id,
            name: entity.name,
            enabled: entity.enabled,
            disabledReason: entity.disabledReason,
            dateAdded: entity.dateAdded
        };
    }
    async list() {
        return _models.Printer.find({}, null, {
            sort: {
                dateAdded: 1
            }
        });
    }
    async get(printerId) {
        const filter = {
            _id: printerId
        };
        const printer = await _models.Printer.findOne(filter);
        if (!printer) {
            throw new _runtimeexceptions.NotFoundException(`The printer with provided id was not found`);
        }
        return printer;
    }
    async create(newPrinter, emitEvent = true) {
        if (!newPrinter) throw new Error("Missing printer to create");
        const mergedPrinter = await this.validateAndDefault(newPrinter);
        mergedPrinter.dateAdded = Date.now();
        const printer = await _models.Printer.create(mergedPrinter);
        if (emitEvent) {
            this.eventEmitter2.emit(_eventconstants.printerEvents.printerCreated, {
                printer
            });
        }
        return printer;
    }
    async update(printerId, updateData) {
        const printer = await this.get(printerId);
        updateData.printerURL = (0, _urlutils.normalizeURLWithProtocol)(updateData.printerURL);
        const { printerURL, apiKey, enabled, name, printerType } = await (0, _validators.validateInput)(updateData, _printerservicevalidation.createMongoPrinterRules);
        printer.printerURL = printerURL;
        printer.apiKey = apiKey;
        if (enabled !== undefined) {
            printer.enabled = enabled;
        }
        printer.name = name;
        printer.printerType = printerType;
        await printer.save();
        this.eventEmitter2.emit(_eventconstants.printerEvents.printerUpdated, {
            printer
        });
        return printer;
    }
    async deleteMany(printerIds, emitEvent = true) {
        await _models.Printer.deleteMany({
            _id: {
                $in: printerIds
            }
        });
        if (emitEvent) {
            this.eventEmitter2.emit(_eventconstants.printerEvents.printersDeleted, {
                printerIds
            });
        }
    }
    async delete(printerId, emitEvent = true) {
        const filter = {
            _id: printerId
        };
        await _models.Printer.findOneAndDelete(filter);
        if (emitEvent) {
            this.eventEmitter2.emit(_eventconstants.printerEvents.printersDeleted, {
                printerIds: [
                    printerId
                ]
            });
        }
    }
    async batchImport(printers) {
        if (!printers?.length) return [];
        for (let printer of printers){
            await this.validateAndDefault(printer);
        }
        this.logger.log("Validation passed");
        const newPrinters = [];
        for (let printer of printers){
            const createdPrinter = await this.create(printer, false);
            newPrinters.push(createdPrinter);
        }
        this.logger.log("Batch create succeeded");
        this.eventEmitter2.emit(_eventconstants.printerEvents.batchPrinterCreated, {
            printers: newPrinters
        });
        return newPrinters;
    }
    async updateFlowRate(printerId, flowRate) {
        const update = {
            flowRate
        };
        await this.get(printerId);
        const printer = await _models.Printer.findByIdAndUpdate(printerId, update, {
            new: true,
            useFindAndModify: false
        });
        this.eventEmitter2.emit(_eventconstants.printerEvents.printerUpdated, {
            printer
        });
        return printer;
    }
    async updateFeedRate(printerId, feedRate) {
        const update = {
            feedRate
        };
        await this.get(printerId);
        const printer = await _models.Printer.findByIdAndUpdate(printerId, update, {
            new: true,
            useFindAndModify: false
        });
        this.eventEmitter2.emit(_eventconstants.printerEvents.printerUpdated, {
            printer
        });
        return printer;
    }
    async updateConnectionSettings(printerId, { printerURL, apiKey, printerType }) {
        const update = {
            printerURL: (0, _urlutils.normalizeURLWithProtocol)(printerURL),
            apiKey,
            printerType
        };
        await (0, _validators.validateInput)(update, _printerservicevalidation.createMongoPrinterRules);
        await this.get(printerId);
        const printer = await _models.Printer.findByIdAndUpdate(printerId, update, {
            new: true,
            useFindAndModify: false
        });
        this.eventEmitter2.emit(_eventconstants.printerEvents.printerUpdated, {
            printer
        });
        return printer;
    }
    async updateEnabled(printerId, enabled) {
        const update = enabled ? {
            enabled,
            disabledReason: null
        } : {
            enabled
        };
        await (0, _validators.validateInput)(update, _printerservicevalidation.updatePrinterEnabledRule);
        await this.get(printerId);
        const printer = await _models.Printer.findByIdAndUpdate(printerId, update, {
            new: true,
            useFindAndModify: false
        });
        this.eventEmitter2.emit(_eventconstants.printerEvents.printerUpdated, {
            printer
        });
        return printer;
    }
    async updateDisabledReason(printerId, disabledReason) {
        const enabled = !disabledReason?.length;
        const update = {
            disabledReason,
            enabled
        };
        await (0, _validators.validateInput)(update, _printerservicevalidation.updatePrinterDisabledReasonRule);
        await this.get(printerId);
        const printer = await _models.Printer.findByIdAndUpdate(printerId, update, {
            new: true,
            useFindAndModify: false
        });
        this.eventEmitter2.emit(_eventconstants.printerEvents.printerUpdated, {
            printer
        });
        return printer;
    }
    async validateAndDefault(printer) {
        const mergedPrinter = {
            enabled: true,
            ...printer
        };
        if (mergedPrinter.printerURL?.length) {
            mergedPrinter.printerURL = (0, _urlutils.normalizeURLWithProtocol)(mergedPrinter.printerURL);
        }
        return await (0, _validators.validateInput)(mergedPrinter, _printerservicevalidation.createMongoPrinterRules);
    }
}

//# sourceMappingURL=printer.service.js.map