"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");
const _uuid = require("uuid");
class SettingsStore {
    settingsService;
    logger;
    settings = null;
    constructor(loggerFactory, settingsService){
        this.settingsService = settingsService;
        this.logger = loggerFactory(SettingsStore.name);
    }
    getSettings() {
        this.throwIfSettingsUnset();
        const settings = this.settings;
        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,
                experimentalPrusaLinkSupport: settings[_serversettingsconstants.serverSettingsKey].experimentalPrusaLinkSupport,
                experimentalBambuSupport: settings[_serversettingsconstants.serverSettingsKey].experimentalBambuSupport
            },
            [_serversettingsconstants.wizardSettingKey]: settings[_serversettingsconstants.wizardSettingKey],
            [_serversettingsconstants.frontendSettingKey]: settings[_serversettingsconstants.frontendSettingKey],
            [_serversettingsconstants.timeoutSettingKey]: settings[_serversettingsconstants.timeoutSettingKey]
        });
    }
    getSettingsSensitive() {
        this.throwIfSettingsUnset();
        const settings = this.settings;
        return Object.freeze({
            [_serversettingsconstants.credentialSettingsKey]: {
                jwtExpiresIn: settings[_serversettingsconstants.credentialSettingsKey].jwtExpiresIn,
                refreshTokenAttempts: settings[_serversettingsconstants.credentialSettingsKey].refreshTokenAttempts,
                refreshTokenExpiry: settings[_serversettingsconstants.credentialSettingsKey].refreshTokenExpiry,
                slicerApiKey: settings[_serversettingsconstants.credentialSettingsKey].slicerApiKey
            },
            [_serversettingsconstants.serverSettingsKey]: {
                loginRequired: settings[_serversettingsconstants.serverSettingsKey].loginRequired,
                registration: settings[_serversettingsconstants.serverSettingsKey].registration,
                sentryDiagnosticsEnabled: settings[_serversettingsconstants.serverSettingsKey].sentryDiagnosticsEnabled,
                experimentalMoonrakerSupport: settings[_serversettingsconstants.serverSettingsKey].experimentalMoonrakerSupport,
                experimentalPrusaLinkSupport: settings[_serversettingsconstants.serverSettingsKey].experimentalPrusaLinkSupport,
                experimentalBambuSupport: settings[_serversettingsconstants.serverSettingsKey].experimentalBambuSupport
            }
        });
    }
    async loadSettings() {
        this.settings = await this.settingsService.getOrCreate();
        await this.processSentryEnabled();
    }
    async getCredentialSettings() {
        this.throwIfSettingsUnset();
        return this.settings[_serversettingsconstants.credentialSettingsKey];
    }
    async getAnonymousDiagnosticsEnabled() {
        this.throwIfSettingsUnset();
        return this.settings[_serversettingsconstants.serverSettingsKey].sentryDiagnosticsEnabled;
    }
    async persistOptionalCredentialSettings(overrideJwtSecret, overrideJwtExpiresIn) {
        this.throwIfSettingsUnset();
        const credentialSettings = await this.getCredentialSettings();
        if (overrideJwtSecret?.length) {
            await this.settingsService.updateJwtSecretCredentialSetting({
                jwtSecret: overrideJwtSecret
            });
        }
        if (overrideJwtExpiresIn?.length) {
            await this.updateCoreCredentialSettings({
                refreshTokenExpiry: credentialSettings.refreshTokenExpiry,
                refreshTokenAttempts: credentialSettings.refreshTokenAttempts,
                jwtExpiresIn: Number.parseInt(overrideJwtExpiresIn)
            });
        }
        this.settings[_serversettingsconstants.credentialSettingsKey] = await this.getCredentialSettings();
    }
    getWizardState() {
        this.throwIfSettingsUnset();
        const settings = this.settings;
        return {
            wizardCompleted: settings[_serversettingsconstants.wizardSettingKey].wizardCompleted,
            wizardVersion: settings[_serversettingsconstants.wizardSettingKey].wizardVersion,
            latestWizardVersion: _serverconstants.AppConstants.currentWizardVersion
        };
    }
    isWizardCompleted() {
        this.throwIfSettingsUnset();
        const settings = this.settings;
        return settings[_serversettingsconstants.wizardSettingKey].wizardCompleted && settings[_serversettingsconstants.wizardSettingKey].wizardVersion === _serverconstants.AppConstants.currentWizardVersion;
    }
    getWizardSettings() {
        this.throwIfSettingsUnset();
        return this.settings[_serversettingsconstants.wizardSettingKey];
    }
    isRegistrationEnabled() {
        this.throwIfSettingsUnset();
        return this.settings[_serversettingsconstants.serverSettingsKey].registration;
    }
    getServerSettings() {
        return this.getSettings()[_serversettingsconstants.serverSettingsKey];
    }
    getTimeoutSettings() {
        return this.getSettings()[_serversettingsconstants.timeoutSettingKey];
    }
    async setWizardCompleted(version) {
        this.settings = await this.settingsService.updateWizardSettings({
            wizardCompleted: true,
            wizardCompletedAt: new Date(),
            wizardVersion: version
        });
        return this.getSettings();
    }
    async getLoginRequired() {
        return this.getServerSettings().loginRequired;
    }
    async setLoginRequired(loginRequired = true) {
        this.throwIfSettingsUnset();
        this.settings[_serversettingsconstants.serverSettingsKey].loginRequired = loginRequired;
        this.settings = await this.settingsService.updateServerSettings(this.settings[_serversettingsconstants.serverSettingsKey]);
        return this.getSettings();
    }
    async setRegistrationEnabled(registration = true) {
        this.throwIfSettingsUnset();
        this.settings[_serversettingsconstants.serverSettingsKey].registration = registration;
        this.settings = await this.settingsService.updateServerSettings(this.settings[_serversettingsconstants.serverSettingsKey]);
        return this.getSettings();
    }
    async updateServerSettings(serverSettings) {
        this.settings = await this.settingsService.updateServerSettings(serverSettings);
        return this.getSettings();
    }
    async updateTimeoutSettings(timeoutSettings) {
        this.settings = await this.settingsService.updateTimeoutSettings(timeoutSettings);
        return this.getSettings();
    }
    async updateCoreCredentialSettings(credentialSettings) {
        this.settings = await this.settingsService.updateCoreCredentialSettings(credentialSettings);
    }
    async setRefreshTokenSettings({ refreshTokenAttempts, refreshTokenExpiry }) {
        this.throwIfSettingsUnset();
        this.settings[_serversettingsconstants.credentialSettingsKey].refreshTokenAttempts = refreshTokenAttempts;
        this.settings[_serversettingsconstants.credentialSettingsKey].refreshTokenExpiry = refreshTokenExpiry;
        await this.updateCoreCredentialSettings(this.settings[_serversettingsconstants.credentialSettingsKey]);
    }
    async setSentryDiagnosticsEnabled(sentryDiagnosticsEnabled) {
        this.throwIfSettingsUnset();
        this.settings[_serversettingsconstants.serverSettingsKey].sentryDiagnosticsEnabled = sentryDiagnosticsEnabled;
        this.settings = await this.settingsService.updateServerSettings(this.settings[_serversettingsconstants.serverSettingsKey]);
        await this.processSentryEnabled();
        return this.getSettings();
    }
    async setExperimentalMoonrakerSupport(experimentalMoonrakerSupport) {
        this.throwIfSettingsUnset();
        this.settings[_serversettingsconstants.serverSettingsKey].experimentalMoonrakerSupport = experimentalMoonrakerSupport;
        this.settings = await this.settingsService.updateServerSettings(this.settings[_serversettingsconstants.serverSettingsKey]);
        return this.getSettings();
    }
    async setExperimentalPrusaLinkSupport(experimentalPrusaLinkSupport) {
        this.throwIfSettingsUnset();
        this.settings[_serversettingsconstants.serverSettingsKey].experimentalPrusaLinkSupport = experimentalPrusaLinkSupport;
        this.settings = await this.settingsService.updateServerSettings(this.settings[_serversettingsconstants.serverSettingsKey]);
        return this.getSettings();
    }
    async setExperimentalBambuSupport(experimentalBambuSupport) {
        this.throwIfSettingsUnset();
        this.settings[_serversettingsconstants.serverSettingsKey].experimentalBambuSupport = experimentalBambuSupport;
        this.settings = await this.settingsService.updateServerSettings(this.settings[_serversettingsconstants.serverSettingsKey]);
        return this.getSettings();
    }
    async updateFrontendSettings(frontendSettings) {
        this.settings = await this.settingsService.updateFrontendSettings(frontendSettings);
        return this.getSettings();
    }
    getSlicerApiKey() {
        this.throwIfSettingsUnset();
        return this.settings[_serversettingsconstants.credentialSettingsKey].slicerApiKey;
    }
    async generateSlicerApiKey() {
        const newApiKey = (0, _uuid.v4)();
        return this.setSlicerApiKey(newApiKey);
    }
    async setSlicerApiKey(apiKey) {
        this.throwIfSettingsUnset();
        this.settings = await this.settingsService.updateSlicerApiKey({
            slicerApiKey: apiKey
        });
        return apiKey;
    }
    async deleteSlicerApiKey() {
        this.throwIfSettingsUnset();
        this.settings = await this.settingsService.updateSlicerApiKey({
            slicerApiKey: null
        });
    }
    validateSlicerApiKey(apiKey) {
        const storedKey = this.getSlicerApiKey();
        if (!storedKey) {
            return false;
        }
        return storedKey === apiKey;
    }
    throwIfSettingsUnset() {
        if (!this.settings) throw new _runtimeexceptions.InternalServerException("Could not check server settings (server settings not loaded)");
    }
    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.getClient)();
        if (!client) {
            this.logger.warn("Could not apply Sentry. Was the SDK initialized?");
            return;
        }
        client.getOptions().enabled = sentryEnabled;
    }
}

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