"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, {
    SettingsController: function() {
        return SettingsController;
    },
    default: function() {
        return _default;
    }
});
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 _ip = require("ip");
const _demomiddleware = require("../middleware/demo.middleware");
const _printerapiinterface = require("../services/printer-api.interface");
class SettingsController {
    settingsStore;
    logger;
    printerCache;
    printerService;
    configService;
    serverVersion;
    constructor({ settingsStore, printerCache, printerService, serverVersion, loggerFactory, configService }){
        this.settingsStore = settingsStore;
        this.logger = loggerFactory(SettingsController.name);
        this.serverVersion = serverVersion;
        this.configService = configService;
        this.printerCache = printerCache;
        this.printerService = printerService;
    }
    getSettings(req, res) {
        let connection;
        try {
            const serverIp = (0, _ip.address)();
            connection = {
                clientIp: req.socket?.remoteAddress,
                ip: serverIp,
                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.sentryDiagnosticsEnabledRules);
        const result = this.settingsStore.setSentryDiagnosticsEnabled(enabled);
        res.send(result);
    }
    async updateMoonrakerSupport(req, res) {
        const { enabled } = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.moonrakerSupportRules);
        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 updateClientSupport(req, res) {
        const { enabled } = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.clientNextRules);
        const result = await this.settingsStore.setExperimentalClientSupport(enabled);
        res.send(result);
    }
    async updateWhitelistSettings(req, res) {
        const { whitelistEnabled, whitelistedIpAddresses } = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.whitelistSettingUpdateRules);
        if (!whitelistedIpAddresses.includes("127.0.0.1")) {
            whitelistedIpAddresses.push("127.0.0.1");
        }
        await this.settingsStore.setWhitelist(whitelistEnabled, whitelistedIpAddresses);
        res.send();
    }
    async updateFrontendSettings(req, res) {
        const validatedInput = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.frontendSettingsUpdateRules);
        const result = await this.settingsStore.updateFrontendSettings(validatedInput);
        res.send(result);
    }
    async updateServerSettings(req, res) {
        const isWhitelistFeatureEnabled = this.configService.get(_serverconstants.AppConstants.ENABLE_EXPERIMENTAL_WHITELIST_SETTINGS, "false") === "true";
        const validatedInput = await (0, _validators.validateInput)(req.body, (0, _settingsservicevalidation.serverSettingsUpdateRules)(isWhitelistFeatureEnabled));
        const result = await this.settingsStore.updateServerSettings(validatedInput);
        res.send(result);
    }
    async updateLoginRequiredSettings(req, res) {
        const { loginRequired } = await (0, _validators.validateInput)(req.body, {
            loginRequired: "required|boolean"
        });
        const result = await this.settingsStore.setLoginRequired(loginRequired);
        res.send(result);
    }
    async updateRegistrationEnabledSettings(req, res) {
        const { registrationEnabled } = await (0, _validators.validateInput)(req.body, {
            registrationEnabled: "required|boolean"
        });
        const result = await this.settingsStore.setRegistrationEnabled(registrationEnabled);
        res.send(result);
    }
    async updateCredentialSettings(req, res) {
        const validatedInput = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.credentialSettingPatchRules);
        await this.settingsStore.updateCredentialSettings(validatedInput);
        res.send();
    }
    async updateFileCleanSettings(req, res) {
        const validatedInput = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.fileCleanSettingsUpdateRules);
        const result = await this.settingsStore.patchFileCleanSettings(validatedInput);
        res.send(result);
    }
    async updateTimeoutSettings(req, res) {
        const validatedInput = await (0, _validators.validateInput)(req.body, _settingsservicevalidation.timeoutSettingsUpdateRules);
        const result = await this.settingsStore.updateTimeoutSettings(validatedInput);
        res.send(result);
    }
}
const _default = (0, _awilixexpress.createController)(SettingsController).prefix(_serverconstants.AppConstants.apiRoute + "/settings").before([
    (0, _authenticate.authenticate)()
]).get("/", "getSettings").get("/sensitive", "getSettingsSensitive", {
    before: [
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]
}).patch("/sentry-diagnostics", "updateSentryDiagnosticsEnabled", _demomiddleware.demoUserNotAllowedInterceptor).put("/experimental-moonraker-support", "updateMoonrakerSupport", {
    before: [
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]
}).put("/experimental-client-support", "updateClientSupport", {
    before: [
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]
}).put("/server", "updateServerSettings", {
    before: [
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]
}).put("/login-required", "updateLoginRequiredSettings", {
    before: [
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]
}).put("/registration-enabled", "updateRegistrationEnabledSettings", {
    before: [
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]
}).put("/credential", "updateCredentialSettings", {
    before: [
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]
}).put("/file-clean", "updateFileCleanSettings", {
    before: [
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]
}).put("/whitelist", "updateWhitelistSettings", {
    before: [
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]
}).put("/frontend", "updateFrontendSettings", {
    before: [
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ])
    ]
}).put("/timeout", "updateTimeoutSettings", {
    before: [
        (0, _authenticate.authorizeRoles)([
            _authorizationconstants.ROLES.ADMIN
        ]),
        _demomiddleware.demoUserNotAllowed
    ]
});

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