"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "BatchCallService", {
    enumerable: true,
    get: function() {
        return BatchCallService;
    }
});
const _node = require("@sentry/node");
const _errorutils = require("../../utils/error.utils");
const _printerapiinterface = require("../printer-api.interface");
class BatchCallService {
    printerApiFactory;
    printerCache;
    printerSocketStore;
    printerService;
    logger;
    constructor(loggerFactory, printerApiFactory, printerCache, printerSocketStore, printerService){
        this.printerApiFactory = printerApiFactory;
        this.printerCache = printerCache;
        this.printerSocketStore = printerSocketStore;
        this.printerService = printerService;
        this.logger = loggerFactory(BatchCallService.name);
    }
    getPrinter(login) {
        return this.printerApiFactory.getScopedPrinter(login);
    }
    async batchTogglePrintersEnabled(printerIds, enabled) {
        const promises = [];
        for (const printerId of printerIds){
            let promise = undefined;
            const printerDto = await this.printerCache.getValue(printerId);
            if (!printerDto) continue;
            const time = Date.now();
            if (enabled) {
                if (!printerDto.enabled && !printerDto.disabledReason?.length) {
                    promise = this.printerService.updateEnabled(printerId, true).then(()=>{
                        return {
                            success: true,
                            printerId,
                            time: Date.now() - time
                        };
                    }).catch((e)=>{
                        return {
                            failure: true,
                            error: e.message,
                            printerId,
                            time: Date.now() - time
                        };
                    });
                }
            } else if (printerDto.enabled) {
                promise = this.printerService.updateEnabled(printerId, false).then(()=>{
                    return {
                        success: true,
                        printerId,
                        time: Date.now() - time
                    };
                }).catch((e)=>{
                    return {
                        failure: true,
                        error: e.message,
                        printerId,
                        time: Date.now() - time
                    };
                });
            } else {
                this.logger.warn("Did not toggle printer enabled, its in maintenance");
            }
            if (promise) {
                promises.push(promise);
            }
        }
        return await Promise.all(promises);
    }
    batchConnectSocket(printerIds) {
        for (const printerId of printerIds){
            try {
                this.printerSocketStore.reconnectPrinterAdapter(printerId);
            } catch (e) {
                (0, _node.captureException)(e);
                this.logger.error(`Error setting socket to reconnect ${(0, _errorutils.errorSummary)(e)}`);
            }
        }
    }
    async batchConnectUsb(printerIds) {
        const promises = [];
        for (const printerId of printerIds){
            const login = await this.printerCache.getLoginDtoAsync(printerId);
            const time = Date.now();
            const client = this.getPrinter(login);
            const promise = client.connect().then(()=>{
                return {
                    success: true,
                    printerId,
                    time: Date.now() - time
                };
            }).catch((e)=>{
                return {
                    failure: true,
                    error: e.message,
                    printerId,
                    time: Date.now() - time
                };
            });
            promises.push(promise);
        }
        return await Promise.all(promises);
    }
    async getBatchPrinterReprintFile(printerIds) {
        const promises = [];
        for (const printerId of printerIds){
            const promise = new Promise(async (resolve, _)=>{
                try {
                    const login = await this.printerCache.getLoginDtoAsync(printerId);
                    const client = this.getPrinter(login);
                    const partialReprintState = await client.getReprintState();
                    return resolve({
                        ...partialReprintState,
                        printerId
                    });
                } catch (e) {
                    (0, _node.captureException)(e);
                    return resolve({
                        connectionState: null,
                        printerId,
                        reprintState: _printerapiinterface.ReprintState.PrinterNotAvailable
                    });
                }
            });
            promises.push(promise);
        }
        return await Promise.all(promises);
    }
    async batchReprintCalls(printerIdFileList) {
        const promises = [];
        for (const printerIdFile of printerIdFileList){
            const { printerId, path } = printerIdFile;
            const login = await this.printerCache.getLoginDtoAsync(printerId);
            const time = Date.now();
            const client = this.getPrinter(login);
            const promise = client.startPrint(path).then(()=>{
                return {
                    success: true,
                    printerId,
                    time: Date.now() - time
                };
            }).catch((e)=>{
                (0, _node.captureException)(e);
                return {
                    failure: true,
                    error: e.message,
                    printerId,
                    time: Date.now() - time
                };
            });
            promises.push(promise);
        }
        return await Promise.all(promises);
    }
}

//# sourceMappingURL=batch-call.service.js.map