"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SettingsController", {
    enumerable: true,
    get: function() {
        return SettingsController;
    }
});
const _awilixexpress = require("awilix-express");
const _authenticate = require("../middleware/authenticate");
const _serverconstants = require("../server.constants");
const _authorizationconstants = require("../constants/authorization.constants");
const _validators = require("../handlers/validators");
const _settingsservicevalidation = require("../services/validators/settings-service.validation");
const _settingsstore = require("../state/settings.store");
const _express = require("express");
const _loggerfactory = require("../handlers/logger-factory");
const _demomiddleware = require("../middleware/demo.middleware");
const _printercache = require("../state/printer.cache");
const _printerapiinterface = require("../services/printer-api.interface");
const _printerserviceinterface = require("../services/interfaces/printer.service.interface");
const _printerthumbnailcache = require("../state/printer-thumbnail.cache");
const _settingvalidation = require("./validation/setting.validation");
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
}
class SettingsController {
    serverVersion;
    printerCache;
    printerService;
    settingsStore;
    printerThumbnailCache;
    logger;
    constructor(loggerFactory, serverVersion, printerCache, printerService, settingsStore, printerThumbnailCache){
        this.serverVersion = serverVersion;
        this.printerCache = printerCache;
        this.printerService = printerService;
        this.settingsStore = settingsStore;
        this.printerThumbnailCache = printerThumbnailCache;
        this.logger = loggerFactory(SettingsController.name);
    }
    async getSettings(req, res) {
        let connection;
        try {
            connection = {
                clientIp: req.socket?.remoteAddress,
                version: this.serverVersion
            };
        } catch (e) {
            this.logger.warn("Could not fetch server IP address");
        }
        const settings = this.settingsStore.getSettings();
        res.send({
            ...settings,
            connection
        });
    }
    async getSettingsSensitive(req, res) {
        const settings = this.settingsStore.getSettingsSensitive();
        res.send(settings);
    }
    async updateSentryDiagnosticsEnabled(req, res) {
        const { enabled } = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.sentryDiagnosticsEnabledSchema);
        const result = this.settingsStore.setSentryDiagnosticsEnabled(enabled);
        res.send(result);
    }
    async updateMoonrakerSupport(req, res) {
        const { enabled } = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.moonrakerSupportSchema);
        const result = await this.settingsStore.setExperimentalMoonrakerSupport(enabled);
        if (!enabled) {
            const printers = await this.printerCache.listCachedPrinters(false);
            const klipperPrinters = printers.filter((p)=>p.printerType === _printerapiinterface.MoonrakerType);
            for (const printer of klipperPrinters){
                await this.printerService.updateEnabled(printer.id, false);
            }
        }
        res.send(result);
    }
    async updatePrusaLinkSupport(req, res) {
        const { enabled } = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.prusaLinkSupportSchema);
        const result = await this.settingsStore.setExperimentalPrusaLinkSupport(enabled);
        if (!enabled) {
            const printers = await this.printerCache.listCachedPrinters(false);
            const prusaLinkPrinters = printers.filter((p)=>p.printerType === _printerapiinterface.PrusaLinkType);
            for (const printer of prusaLinkPrinters){
                await this.printerService.updateEnabled(printer.id, false);
            }
        }
        res.send(result);
    }
    async updateBambuSupport(req, res) {
        const { enabled } = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.bambuSupportSchema);
        const result = await this.settingsStore.setExperimentalBambuSupport(enabled);
        if (!enabled) {
            const printers = await this.printerCache.listCachedPrinters(false);
            const bambuPrinters = printers.filter((p)=>p.printerType === _printerapiinterface.BambuType);
            for (const printer of bambuPrinters){
                await this.printerService.updateEnabled(printer.id, false);
            }
        }
        res.send(result);
    }
    async updateFrontendSettings(req, res) {
        const validatedInput = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.frontendSettingsUpdateSchema);
        const result = await this.settingsStore.updateFrontendSettings(validatedInput);
        res.send(result);
    }
    async updateLoginRequiredSettings(req, res) {
        const { loginRequired } = await (0, _validators.validateInput)(req.body, _settingvalidation.loginRequiredSchema);
        const result = await this.settingsStore.setLoginRequired(loginRequired);
        res.send(result);
    }
    async updateRegistrationEnabledSettings(req, res) {
        const { registrationEnabled } = await (0, _validators.validateInput)(req.body, _settingvalidation.registrationEnabledSchema);
        const result = await this.settingsStore.setRegistrationEnabled(registrationEnabled);
        res.send(result);
    }
    async updateCredentialSettings(req, res) {
        const validatedInput = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.credentialCoreSettingUpdateSchema);
        await this.settingsStore.updateCoreCredentialSettings(validatedInput);
        res.send();
    }
    async updateTimeoutSettings(req, res) {
        const validatedInput = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.timeoutSettingsUpdateSchema);
        const result = await this.settingsStore.updateTimeoutSettings(validatedInput);
        res.send(result);
    }
    async getSlicerApiKey(req, res) {
        const apiKey = this.settingsStore.getSlicerApiKey();
        res.send({
            slicerApiKey: apiKey
        });
    }
    async regenerateSlicerApiKey(req, res) {
        const newApiKey = await this.settingsStore.generateSlicerApiKey();
        this.logger.log("Slicer API key regenerated");
        res.send({
            slicerApiKey: newApiKey
        });
    }
    async deleteSlicerApiKey(req, res) {
        await this.settingsStore.deleteSlicerApiKey();
        this.logger.log("Slicer API key deleted");
        res.send({
            slicerApiKey: null
        });
    }
}
_ts_decorate([
    (0, _awilixexpress.GET)(),
    (0, _awilixexpress.route)("/"),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "getSettings", null);
_ts_decorate([
    (0, _awilixexpress.GET)(),
    (0, _awilixexpress.route)("/sensitive"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "getSettingsSensitive", null);
_ts_decorate([
    (0, _awilixexpress.PATCH)(),
    (0, _awilixexpress.route)("/sentry-diagnostics"),
    (0, _awilixexpress.before)([
        _demomiddleware.demoUserNotAllowed
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "updateSentryDiagnosticsEnabled", null);
_ts_decorate([
    (0, _awilixexpress.PUT)(),
    (0, _awilixexpress.route)("/experimental-moonraker-support"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "updateMoonrakerSupport", null);
_ts_decorate([
    (0, _awilixexpress.PUT)(),
    (0, _awilixexpress.route)("/experimental-prusa-link-support"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "updatePrusaLinkSupport", null);
_ts_decorate([
    (0, _awilixexpress.PUT)(),
    (0, _awilixexpress.route)("/experimental-bambu-support"),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "updateBambuSupport", null);
_ts_decorate([
    (0, _awilixexpress.PUT)(),
    (0, _awilixexpress.route)("/frontend"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ])
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "updateFrontendSettings", null);
_ts_decorate([
    (0, _awilixexpress.PUT)(),
    (0, _awilixexpress.route)("/login-required"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "updateLoginRequiredSettings", null);
_ts_decorate([
    (0, _awilixexpress.PUT)(),
    (0, _awilixexpress.route)("/registration-enabled"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "updateRegistrationEnabledSettings", null);
_ts_decorate([
    (0, _awilixexpress.PUT)(),
    (0, _awilixexpress.route)("/credential"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "updateCredentialSettings", null);
_ts_decorate([
    (0, _awilixexpress.PUT)(),
    (0, _awilixexpress.route)("/timeout"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "updateTimeoutSettings", null);
_ts_decorate([
    (0, _awilixexpress.GET)(),
    (0, _awilixexpress.route)("/slicer-api-key"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "getSlicerApiKey", null);
_ts_decorate([
    (0, _awilixexpress.POST)(),
    (0, _awilixexpress.route)("/slicer-api-key/regenerate"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "regenerateSlicerApiKey", null);
_ts_decorate([
    (0, _awilixexpress.DELETE)(),
    (0, _awilixexpress.route)("/slicer-api-key"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _express.Request === "undefined" ? Object : _express.Request,
        typeof _express.Response === "undefined" ? Object : _express.Response
    ]),
    _ts_metadata("design:returntype", Promise)
], SettingsController.prototype, "deleteSlicerApiKey", null);
SettingsController = _ts_decorate([
    (0, _awilixexpress.route)(_serverconstants.AppConstants.apiRoute + "/settings"),
    (0, _awilixexpress.before)([
        (0, _authenticate.authenticate)()
    ]),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        typeof _loggerfactory.ILoggerFactory === "undefined" ? Object : _loggerfactory.ILoggerFactory,
        String,
        typeof _printercache.PrinterCache === "undefined" ? Object : _printercache.PrinterCache,
        typeof _printerserviceinterface.IPrinterService === "undefined" ? Object : _printerserviceinterface.IPrinterService,
        typeof _settingsstore.SettingsStore === "undefined" ? Object : _settingsstore.SettingsStore,
        typeof _printerthumbnailcache.PrinterThumbnailCache === "undefined" ? Object : _printerthumbnailcache.PrinterThumbnailCache
    ])
], SettingsController);

//# sourceMappingURL=settings.controller.js.map