"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get currentPrinterToken () {
        return currentPrinterToken;
    },
    get printerApiToken () {
        return printerApiToken;
    },
    get printerIdToken () {
        return printerIdToken;
    },
    get printerLoginToken () {
        return printerLoginToken;
    },
    get printerResolveMiddleware () {
        return printerResolveMiddleware;
    }
});
const _awilix = require("awilix");
const _containertokens = require("../container.tokens");
const _printerapiinterface = require("../services/printer-api.interface");
const printerIdToken = "currentPrinterId";
const printerApiToken = "printerApi";
const currentPrinterToken = "currentPrinter";
const printerLoginToken = "printerLogin";
const printerResolveMiddleware = (key = "id")=>{
    return (req, res, next)=>{
        const printerCache = req.container.resolve(_containertokens.DITokens.printerCache);
        const settingsService = req.container.resolve(_containertokens.DITokens.settingsStore);
        const moonrakerEnabled = settingsService.getServerSettings().experimentalMoonrakerSupport;
        const prusaLinkEnabled = settingsService.getServerSettings().experimentalPrusaLinkSupport;
        const bambuEnabled = settingsService.getServerSettings().experimentalBambuSupport;
        let scopedPrinter = undefined;
        let loginDto = undefined;
        const printerIdParam = req.params[key];
        if (printerIdParam) {
            const printerId = Number.parseInt(printerIdParam, 10);
            scopedPrinter = printerCache.getCachedPrinterOrThrow(printerId);
            loginDto = printerCache.getLoginDto(printerId);
            req.container.register({
                [currentPrinterToken]: (0, _awilix.asValue)(scopedPrinter),
                [printerLoginToken]: (0, _awilix.asValue)(loginDto),
                [printerIdToken]: (0, _awilix.asValue)(printerId)
            });
            switch(scopedPrinter.printerType){
                case _printerapiinterface.OctoprintType:
                    {
                        const octoprintApiInstance = req.container.resolve(_containertokens.DITokens.octoprintApi);
                        req.container.register({
                            [printerApiToken]: (0, _awilix.asValue)(octoprintApiInstance)
                        });
                        break;
                    }
                case _printerapiinterface.MoonrakerType:
                    {
                        const moonrakerInstance = req.container.resolve(_containertokens.DITokens.moonrakerApi);
                        req.container.register({
                            [printerApiToken]: moonrakerEnabled ? (0, _awilix.asValue)(moonrakerInstance) : (0, _awilix.asValue)(undefined)
                        });
                        break;
                    }
                case _printerapiinterface.PrusaLinkType:
                    {
                        const prusaLinkInstance = req.container.resolve(_containertokens.DITokens.prusaLinkApi);
                        req.container.register({
                            [printerApiToken]: prusaLinkEnabled ? (0, _awilix.asValue)(prusaLinkInstance) : (0, _awilix.asValue)(undefined)
                        });
                        break;
                    }
                case _printerapiinterface.BambuType:
                    {
                        const bambuInstance = req.container.resolve(_containertokens.DITokens.bambuApi);
                        bambuInstance.setPrinterId(printerId);
                        req.container.register({
                            [printerApiToken]: bambuEnabled ? (0, _awilix.asValue)(bambuInstance) : (0, _awilix.asValue)(undefined)
                        });
                        break;
                    }
            }
        } else {
            req.container.register({
                [currentPrinterToken]: (0, _awilix.asValue)(undefined),
                [printerLoginToken]: (0, _awilix.asValue)(undefined),
                [printerIdToken]: (0, _awilix.asValue)(undefined),
                [printerApiToken]: (0, _awilix.asValue)(undefined)
            });
        }
        next();
    };
};

//# sourceMappingURL=printer.js.map