"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PrinterService", {
    enumerable: true,
    get: function() {
        return PrinterService;
    }
});
const _printerdto = require("../interfaces/printer.dto");
const _printerentity = require("../../entities/printer.entity");
const _baseservice = require("./base.service");
const _validators = require("../../handlers/validators");
const _eventconstants = require("../../constants/event.constants");
const _normalizeurl = require("../../utils/normalize-url");
const _urlutils = require("../../utils/url.utils");
const _printerservicevalidation = require("../validators/printer-service.validation");
class PrinterService extends (0, _baseservice.BaseService)(_printerentity.Printer, _printerdto.PrinterDto, _printerdto.CreatePrinterDto) {
    eventEmitter2;
    logger;
    constructor(loggerFactory, typeormService, eventEmitter2){
        super(typeormService), this.eventEmitter2 = eventEmitter2;
        this.logger = loggerFactory(PrinterService.name);
    }
    toDto(entity) {
        return {
            id: entity.id,
            name: entity.name,
            enabled: entity.enabled,
            disabledReason: entity.disabledReason,
            dateAdded: entity.dateAdded,
            apiKey: entity.apiKey,
            username: entity.username,
            password: entity.password,
            printerURL: entity.printerURL,
            printerType: entity.printerType
        };
    }
    async list() {
        return this.repository.find({
            order: {
                dateAdded: "ASC"
            }
        });
    }
    async create(newPrinter, emitEvent = true) {
        const mergedPrinter = await this.validateAndDefault(newPrinter);
        mergedPrinter.dateAdded = Date.now();
        const printer = await super.create(mergedPrinter);
        if (emitEvent) {
            this.eventEmitter2.emit(_eventconstants.printerEvents.printerCreated, {
                printer
            });
        }
        return printer;
    }
    async update(printerId, partial) {
        const printer = await this.get(printerId);
        if (partial.printerURL) {
            partial.printerURL = (0, _normalizeurl.normalizeUrl)(partial.printerURL, {
                defaultProtocol: _urlutils.defaultHttpProtocol
            });
        }
        Object.assign(printer, partial);
        const { printerURL, apiKey, enabled, name, printerType, password, username } = await (0, _validators.validateInput)(printer, _printerservicevalidation.createPrinterSchema);
        const updatedPrinter = await super.update(printerId, {
            printerURL,
            name,
            apiKey,
            enabled,
            printerType,
            password,
            username
        });
        this.eventEmitter2.emit(_eventconstants.printerEvents.printerUpdated, {
            printer
        });
        return updatedPrinter;
    }
    async batchImport(printers) {
        if (!printers?.length) return [];
        this.logger.log("Validation passed");
        for (let printer of printers){
            await this.validateAndDefault(printer);
        }
        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 delete(printerId, emitEvent = true) {
        await this.repository.delete([
            printerId
        ]);
        if (emitEvent) {
            this.eventEmitter2.emit(_eventconstants.printerEvents.printersDeleted, {
                printerIds: [
                    printerId
                ]
            });
        }
    }
    async deleteMany(printerIds, emitEvent = true) {
        await this.repository.delete(printerIds);
        if (emitEvent) {
            this.eventEmitter2.emit(_eventconstants.printerEvents.printersDeleted, {
                printerIds
            });
        }
    }
    updateDisabledReason(printerId, disabledReason) {
        return this.update(printerId, {
            disabledReason
        });
    }
    updateEnabled(printerId, enabled) {
        return this.update(printerId, {
            enabled
        });
    }
    updateFeedRate(printerId, feedRate) {
        return this.update(printerId, {
            feedRate
        });
    }
    updateFlowRate(printerId, flowRate) {
        return this.update(printerId, {
            flowRate
        });
    }
    async validateAndDefault(printer) {
        const mergedPrinter = {
            enabled: true,
            ...printer
        };
        if (mergedPrinter.printerURL?.length) {
            mergedPrinter.printerURL = (0, _normalizeurl.normalizeUrl)(mergedPrinter.printerURL, {
                defaultProtocol: _urlutils.defaultHttpProtocol
            });
        }
        return await (0, _validators.validateInput)(mergedPrinter, _printerservicevalidation.createPrinterSchema);
    }
}

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