"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PrinterCache", {
    enumerable: true,
    get: function() {
        return PrinterCache;
    }
});
const _keydiffcache = require("../utils/cache/key-diff.cache");
const _eventconstants = require("../constants/event.constants");
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
class PrinterCache extends _keydiffcache.KeyDiffCache {
    printerService;
    eventEmitter2;
    constructor({ printerService, eventEmitter2 }){
        super();
        this.printerService = printerService;
        this.eventEmitter2 = eventEmitter2;
        this.eventEmitter2.on(_eventconstants.printerEvents.batchPrinterCreated, this.handleBatchPrinterCreated.bind(this));
        this.eventEmitter2.on(_eventconstants.printerEvents.printerCreated, this.handlePrinterCreatedOrUpdated.bind(this));
        this.eventEmitter2.on(_eventconstants.printerEvents.printerUpdated, this.handlePrinterCreatedOrUpdated.bind(this));
        this.eventEmitter2.on(_eventconstants.printerEvents.printersDeleted, this.handlePrintersDeleted.bind(this));
    }
    async loadCache() {
        const printerDocs = await this.printerService.list();
        const dtos = this.mapArray(printerDocs);
        const keyValues = dtos.map((p)=>({
                key: this.getId(p),
                value: p
            }));
        await this.setKeyValuesBatch(keyValues, true);
        return dtos;
    }
    async countDisabledPrinters() {
        return (await this.getAllValues()).filter((p)=>!p.enabled).length;
    }
    async listCachedPrinters(includeDisabled = false) {
        const printers = await this.getAllValues();
        if (!includeDisabled) {
            return printers.filter((p)=>p.enabled);
        }
        return printers;
    }
    async getCachedPrinterOrThrowAsync(id) {
        const printer = await this.getValue(id);
        if (!printer) {
            throw new _runtimeexceptions.NotFoundException(`Printer with provided id not found`);
        }
        return printer;
    }
    getCachedPrinterOrThrow(id) {
        const printer = this.keyValueStore[id];
        if (!printer) {
            throw new _runtimeexceptions.NotFoundException(`Printer with provided id not found`);
        }
        return printer;
    }
    async getNameAsync(id) {
        const printer = await this.getCachedPrinterOrThrowAsync(id);
        return printer.name;
    }
    getName(id) {
        const printer = this.getCachedPrinterOrThrow(id);
        return printer.name;
    }
    async getLoginDtoAsync(id) {
        const printer = await this.getCachedPrinterOrThrowAsync(id);
        return {
            printerURL: printer.printerURL,
            apiKey: printer.apiKey,
            printerType: printer.printerType
        };
    }
    getLoginDto(id) {
        const printer = this.getCachedPrinterOrThrow(id);
        return {
            printerURL: printer.printerURL,
            apiKey: printer.apiKey,
            printerType: printer.printerType
        };
    }
    async handleBatchPrinterCreated({ printers }) {
        const mappedPrinters = this.mapArray(printers);
        const keyValues = mappedPrinters.map((p)=>({
                key: this.getId(p),
                value: p
            }));
        await this.setKeyValuesBatch(keyValues, true);
    }
    async handlePrinterCreatedOrUpdated({ printer }) {
        const printerDto = this.map(printer);
        await this.setKeyValue(printerDto.id, printerDto, true);
    }
    async handlePrintersDeleted({ printerIds }) {
        await this.deleteKeysBatch(printerIds, true);
    }
    getId(value) {
        return value.id.toString();
    }
    mapArray(entities) {
        return entities.map((p)=>{
            return this.map(p);
        });
    }
    map(entity) {
        return this.printerService.toUnsafeDto(entity);
    }
}

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