"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SettingsStore", {
    enumerable: true,
    get: function() {
        return SettingsStore;
    }
});
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
const _serversettingsconstants = require("../constants/server-settings.constants");
const _node = require("@sentry/node");
const _envutils = require("../utils/env.utils");
const _serverconstants = require("../server.constants");
class SettingsStore {
    isTypeOrmMode;
    settingsService;
    logger;
    settings = null;
    constructor({ settingsService, loggerFactory, isTypeormMode }){
        this.settingsService = settingsService;
        this.logger = loggerFactory(SettingsStore.name);
        this.isTypeOrmMode = isTypeormMode;
    }
    getSettings() {
        const settings = this.settings;
        if (!settings) throw new _runtimeexceptions.InternalServerException("Could not check server settings (server settings not loaded");
        return Object.freeze({
            [_serversettingsconstants.serverSettingsKey]: {
                loginRequired: settings[_serversettingsconstants.serverSettingsKey].loginRequired,
                registration: settings[_serversettingsconstants.serverSettingsKey].registration,
                sentryDiagnosticsEnabled: settings[_serversettingsconstants.serverSettingsKey].sentryDiagnosticsEnabled,
                experimentalMoonrakerSupport: settings[_serversettingsconstants.serverSettingsKey].experimentalMoonrakerSupport,
                experimentalTypeormSupport: this.isTypeOrmMode,
                experimentalClientSupport: settings[_serversettingsconstants.serverSettingsKey].experimentalClientSupport
            },
            [_serversettingsconstants.wizardSettingKey]: settings[_serversettingsconstants.wizardSettingKey],
            [_serversettingsconstants.frontendSettingKey]: settings[_serversettingsconstants.frontendSettingKey],
            [_serversettingsconstants.printerFileCleanSettingKey]: settings[_serversettingsconstants.printerFileCleanSettingKey],
            [_serversettingsconstants.timeoutSettingKey]: settings[_serversettingsconstants.timeoutSettingKey]
        });
    }
    getSettingsSensitive() {
        const settings = this.settings;
        if (!settings) throw new _runtimeexceptions.InternalServerException("Could not check server settings (server settings not loaded");
        return Object.freeze({
            [_serversettingsconstants.credentialSettingsKey]: {
                jwtExpiresIn: settings[_serversettingsconstants.credentialSettingsKey].jwtExpiresIn,
                refreshTokenAttempts: settings[_serversettingsconstants.credentialSettingsKey].refreshTokenAttempts,
                refreshTokenExpiry: settings[_serversettingsconstants.credentialSettingsKey].refreshTokenExpiry
            },
            [_serversettingsconstants.serverSettingsKey]: {
                debugSettings: settings[_serversettingsconstants.serverSettingsKey].debugSettings,
                experimentalMoonrakerSupport: settings[_serversettingsconstants.serverSettingsKey].experimentalMoonrakerSupport,
                experimentalTypeormSupport: this.isTypeOrmMode,
                experimentalClientSupport: settings[_serversettingsconstants.serverSettingsKey].experimentalClientSupport
            }
        });
    }
    getDebugSettingsSensitive() {
        return this.getSettingsSensitive()[_serversettingsconstants.serverSettingsKey].debugSettings;
    }
    async loadSettings() {
        this.settings = await this.settingsService.getOrCreate();
        await this.processSentryEnabled();
    }
    async getCredentialSettings() {
        return this.settings[_serversettingsconstants.credentialSettingsKey];
    }
    async getAnonymousDiagnosticsEnabled() {
        return this.settings[_serversettingsconstants.serverSettingsKey].sentryDiagnosticsEnabled;
    }
    async persistOptionalCredentialSettings(overrideJwtSecret, overrideJwtExpiresIn) {
        if (overrideJwtSecret) {
            await this.updateCredentialSettings({
                jwtSecret: overrideJwtSecret
            });
        }
        if (overrideJwtExpiresIn) {
            await this.updateCredentialSettings({
                jwtExpiresIn: parseInt(overrideJwtExpiresIn)
            });
        }
    }
    getWizardState() {
        return {
            wizardCompleted: this.settings[_serversettingsconstants.wizardSettingKey].wizardCompleted,
            wizardVersion: this.settings[_serversettingsconstants.wizardSettingKey].wizardVersion,
            latestWizardVersion: _serverconstants.AppConstants.currentWizardVersion
        };
    }
    isWizardCompleted() {
        return this.settings[_serversettingsconstants.wizardSettingKey].wizardCompleted && this.settings[_serversettingsconstants.wizardSettingKey].wizardVersion === _serverconstants.AppConstants.currentWizardVersion;
    }
    getWizardSettings() {
        return this.settings[_serversettingsconstants.wizardSettingKey];
    }
    isRegistrationEnabled() {
        if (!this.settings) throw new _runtimeexceptions.InternalServerException("Could not check server settings (server settings not loaded");
        return this.settings[_serversettingsconstants.serverSettingsKey].registration;
    }
    getServerSettings() {
        return this.getSettings()[_serversettingsconstants.serverSettingsKey];
    }
    getFrontendSettings() {
        return this.getSettings()[_serversettingsconstants.frontendSettingKey];
    }
    getFileCleanSettings() {
        return this.getSettings()[_serversettingsconstants.printerFileCleanSettingKey];
    }
    isPreUploadFileCleanEnabled() {
        return this.getSettings()[_serversettingsconstants.printerFileCleanSettingKey]?.autoRemoveOldFilesBeforeUpload;
    }
    async setWizardCompleted(version) {
        this.settings = await this.settingsService.patchWizardSettings({
            wizardCompleted: true,
            wizardCompletedAt: new Date(),
            wizardVersion: version
        });
        return this.getSettings();
    }
    async setRegistrationEnabled(registration = true) {
        this.settings = await this.settingsService.patchServerSettings({
            registration
        });
        return this.getSettings();
    }
    async getLoginRequired() {
        return this.getServerSettings().loginRequired;
    }
    async setLoginRequired(loginRequired = true) {
        this.settings = await this.settingsService.patchServerSettings({
            loginRequired
        });
        return this.getSettings();
    }
    async updateServerSettings(serverSettings) {
        this.settings = await this.settingsService.patchServerSettings(serverSettings);
        return this.getSettings();
    }
    async updateTimeoutSettings(timeoutSettings) {
        this.settings = await this.settingsService.updateTimeoutSettings(timeoutSettings);
        return this.getSettings();
    }
    async patchFileCleanSettings(fileClean) {
        this.settings = await this.settingsService.patchFileCleanSettings(fileClean);
        return this.getSettings();
    }
    async updateCredentialSettings(credentialSettings) {
        this.settings = await this.settingsService.patchCredentialSettings(credentialSettings);
        return this.getSettings();
    }
    async setSentryDiagnosticsEnabled(sentryDiagnosticsEnabled) {
        this.settings = await this.settingsService.patchServerSettings({
            sentryDiagnosticsEnabled
        });
        await this.processSentryEnabled();
        return this.getSettings();
    }
    async setExperimentalMoonrakerSupport(moonrakerEnabled) {
        this.settings = await this.settingsService.patchServerSettings({
            experimentalMoonrakerSupport: moonrakerEnabled
        });
        return this.getSettings();
    }
    async setExperimentalClientSupport(experimentalClientEnabled) {
        this.settings = await this.settingsService.patchServerSettings({
            experimentalClientSupport: experimentalClientEnabled
        });
        return this.getSettings();
    }
    async updateFrontendSettings(frontendSettings) {
        this.settings = await this.settingsService.updateFrontendSettings(frontendSettings);
        return this.getSettings();
    }
    async processSentryEnabled() {
        const sentryEnabled = await this.getAnonymousDiagnosticsEnabled();
        if (sentryEnabled) {
            this.logger.log("Enabling Sentry for remote diagnostics");
        } else {
            this.logger.log("Disabling Sentry for remote diagnostics");
        }
        if ((0, _envutils.isTestEnvironment)()) return;
        const client = (0, _node.getCurrentHub)().getClient();
        if (!client) {
            this.logger.warn("Could not apply Sentry. Was the SDK initialized?");
            return;
        }
        client.getOptions().enabled = sentryEnabled;
    }
}

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