"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SettingsService", {
    enumerable: true,
    get: function() {
        return SettingsService;
    }
});
const _models = require("../../models");
const _serversettingsconstants = require("../../constants/server-settings.constants");
const _validators = require("../../handlers/validators");
const _settingsservicevalidation = require("../validators/settings-service.validation");
class SettingsService {
    configService;
    constructor({ configService }){
        this.configService = configService;
    }
    toDto(entity) {
        return {
            [_serversettingsconstants.serverSettingsKey]: {
                ...entity[_serversettingsconstants.serverSettingsKey],
                experimentalTypeormSupport: false
            },
            [_serversettingsconstants.wizardSettingKey]: entity[_serversettingsconstants.wizardSettingKey],
            [_serversettingsconstants.frontendSettingKey]: entity[_serversettingsconstants.frontendSettingKey],
            [_serversettingsconstants.printerFileCleanSettingKey]: entity[_serversettingsconstants.printerFileCleanSettingKey],
            [_serversettingsconstants.timeoutSettingKey]: entity[_serversettingsconstants.timeoutSettingKey]
        };
    }
    async getOrCreate() {
        let settings = await _models.Settings.findOne();
        if (!settings) {
            const defaultSettings = new _models.Settings((0, _serversettingsconstants.getDefaultSettings)());
            await defaultSettings.save();
            return defaultSettings;
        } else {
            settings = this.migrateSettingsRuntime(settings);
            return _models.Settings.findOneAndUpdate({
                _id: settings.id
            }, settings, {
                new: true
            });
        }
    }
    migrateSettingsRuntime(knownSettings) {
        const doc = knownSettings;
        if (!doc[_serversettingsconstants.printerFileCleanSettingKey]) {
            doc[_serversettingsconstants.printerFileCleanSettingKey] = (0, _serversettingsconstants.getDefaultFileCleanSettings)();
        } else {
            doc[_serversettingsconstants.printerFileCleanSettingKey] = {
                autoRemoveOldFilesBeforeUpload: doc[_serversettingsconstants.printerFileCleanSettingKey].autoRemoveOldFilesBeforeUpload,
                autoRemoveOldFilesAtBoot: doc[_serversettingsconstants.printerFileCleanSettingKey].autoRemoveOldFilesBeforeUpload,
                autoRemoveOldFilesCriteriumDays: doc[_serversettingsconstants.printerFileCleanSettingKey].autoRemoveOldFilesCriteriumDays
            };
        }
        if (!doc[_serversettingsconstants.wizardSettingKey]) {
            doc[_serversettingsconstants.wizardSettingKey] = (0, _serversettingsconstants.getDefaultWizardSettings)();
        }
        if (!doc[_serversettingsconstants.timeoutSettingKey]) {
            doc[_serversettingsconstants.timeoutSettingKey] = (0, _serversettingsconstants.getDefaultTimeout)();
        }
        if (!doc[_serversettingsconstants.serverSettingsKey]) {
            doc[_serversettingsconstants.serverSettingsKey] = (0, _serversettingsconstants.getDefaultServerSettings)();
        } else {
            doc[_serversettingsconstants.serverSettingsKey] = {
                debugSettings: doc[_serversettingsconstants.serverSettingsKey].debugSettings,
                loginRequired: doc[_serversettingsconstants.serverSettingsKey].loginRequired,
                registration: doc[_serversettingsconstants.serverSettingsKey].registration,
                experimentalClientSupport: doc[_serversettingsconstants.serverSettingsKey].experimentalClientSupport,
                experimentalMoonrakerSupport: doc[_serversettingsconstants.serverSettingsKey].experimentalMoonrakerSupport,
                sentryDiagnosticsEnabled: doc[_serversettingsconstants.serverSettingsKey].sentryDiagnosticsEnabled
            };
        }
        if (!doc[_serversettingsconstants.credentialSettingsKey]) {
            doc[_serversettingsconstants.credentialSettingsKey] = (0, _serversettingsconstants.getDefaultCredentialSettings)();
        }
        if (!doc[_serversettingsconstants.frontendSettingKey]) {
            doc[_serversettingsconstants.frontendSettingKey] = (0, _serversettingsconstants.getDefaultFrontendSettings)();
        }
        return knownSettings;
    }
    async patchFileCleanSettings(patchUpdate) {
        const validatedInput = await (0, _validators.validateInput)(patchUpdate, _settingsservicevalidation.fileCleanSettingsUpdateRules);
        const settingsDoc = await this.getOrCreate();
        settingsDoc[_serversettingsconstants.printerFileCleanSettingKey] = Object.assign(settingsDoc[_serversettingsconstants.printerFileCleanSettingKey], validatedInput);
        return _models.Settings.findOneAndUpdate({
            _id: settingsDoc.id
        }, settingsDoc, {
            new: true
        });
    }
    async patchWizardSettings(patchUpdate) {
        const validatedInput = await (0, _validators.validateInput)(patchUpdate, _settingsservicevalidation.wizardUpdateRules);
        const settingsDoc = await this.getOrCreate();
        settingsDoc[_serversettingsconstants.wizardSettingKey] = Object.assign(settingsDoc[_serversettingsconstants.wizardSettingKey], validatedInput);
        return _models.Settings.findOneAndUpdate({
            _id: settingsDoc.id
        }, settingsDoc, {
            new: true
        });
    }
    async updateFrontendSettings(patchUpdate) {
        const validatedInput = await (0, _validators.validateInput)(patchUpdate, _settingsservicevalidation.frontendSettingsUpdateRules);
        const settingsDoc = await this.getOrCreate();
        const frontendSettings = settingsDoc[_serversettingsconstants.frontendSettingKey];
        Object.assign(frontendSettings, validatedInput);
        return _models.Settings.findOneAndUpdate({
            _id: settingsDoc.id
        }, settingsDoc, {
            new: true
        });
    }
    async patchCredentialSettings(patchUpdate) {
        const validatedInput = await (0, _validators.validateInput)(patchUpdate, _settingsservicevalidation.credentialSettingPatchRules);
        const settingsDoc = await this.getOrCreate();
        const credentialSettings = settingsDoc[_serversettingsconstants.credentialSettingsKey];
        Object.assign(credentialSettings, validatedInput);
        return _models.Settings.findOneAndUpdate({
            _id: settingsDoc.id
        }, settingsDoc, {
            new: true
        });
    }
    async patchServerSettings(patchUpdate) {
        const validatedInput = await (0, _validators.validateInput)(patchUpdate, _settingsservicevalidation.serverSettingsUpdateRules);
        const settingsDoc = await this.getOrCreate();
        const serverSettings = settingsDoc[_serversettingsconstants.serverSettingsKey];
        Object.assign(serverSettings, validatedInput);
        return _models.Settings.findOneAndUpdate({
            _id: settingsDoc.id
        }, settingsDoc, {
            new: true
        });
    }
    async updateTimeoutSettings(patchUpdate) {
        const validatedInput = await (0, _validators.validateInput)(patchUpdate, _settingsservicevalidation.timeoutSettingsUpdateRules);
        const settingsDoc = await this.getOrCreate();
        const timeoutSettings = settingsDoc[_serversettingsconstants.timeoutSettingKey];
        Object.assign(timeoutSettings, validatedInput);
        return _models.Settings.findOneAndUpdate({
            _id: settingsDoc.id
        }, settingsDoc, {
            new: true
        });
    }
}

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