"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    currentPrinterToken: function() {
        return currentPrinterToken;
    },
    printerApiToken: function() {
        return printerApiToken;
    },
    printerIdToken: function() {
        return printerIdToken;
    },
    printerLoginToken: function() {
        return printerLoginToken;
    },
    printerResolveMiddleware: function() {
        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;
        let scopedPrinter = undefined;
        let loginDto = undefined;
        const printerId = req.params[key];
        if (printerId) {
            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)
            });
            const octoprintApiInstance = req.container.resolve(_containertokens.DITokens.octoprintApi);
            switch(scopedPrinter.printerType){
                case _printerapiinterface.OctoprintType:
                    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;
            }
        } 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