"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SettingsService2", {
    enumerable: true,
    get: function() {
        return SettingsService2;
    }
});
const _entities = require("../../entities");
const _serversettingsconstants = require("../../constants/server-settings.constants");
const _baseservice = require("./base.service");
const _settingsdto = require("../interfaces/settings.dto");
class SettingsService2 extends (0, _baseservice.BaseService)(_entities.Settings, _settingsdto.SettingsDto) {
    configService;
    constructor({ configService, typeormService }){
        super({
            typeormService
        });
        this.configService = configService;
    }
    toDto(entity) {
        return {
            [_serversettingsconstants.serverSettingsKey]: {
                ...entity[_serversettingsconstants.serverSettingsKey],
                experimentalTypeormSupport: true
            },
            [_serversettingsconstants.frontendSettingKey]: entity[_serversettingsconstants.frontendSettingKey],
            [_serversettingsconstants.printerFileCleanSettingKey]: entity[_serversettingsconstants.printerFileCleanSettingKey],
            [_serversettingsconstants.wizardSettingKey]: entity[_serversettingsconstants.wizardSettingKey],
            [_serversettingsconstants.timeoutSettingKey]: entity[_serversettingsconstants.timeoutSettingKey]
        };
    }
    async getOrCreate() {
        let settings = await this.get();
        if (!settings) {
            const settings = await this.create({
                [_serversettingsconstants.serverSettingsKey]: (0, _serversettingsconstants.getDefaultServerSettings)(),
                [_serversettingsconstants.credentialSettingsKey]: (0, _serversettingsconstants.getDefaultCredentialSettings)(),
                [_serversettingsconstants.wizardSettingKey]: (0, _serversettingsconstants.getDefaultWizardSettings)(),
                [_serversettingsconstants.printerFileCleanSettingKey]: (0, _serversettingsconstants.getDefaultFileCleanSettings)(),
                [_serversettingsconstants.frontendSettingKey]: (0, _serversettingsconstants.getDefaultFrontendSettings)(),
                [_serversettingsconstants.timeoutSettingKey]: (0, _serversettingsconstants.getDefaultTimeout)()
            });
            return settings;
        } else {
            settings = this.migrateSettingsRuntime(settings);
        }
        return settings;
    }
    async getServerSettings() {
        const settings = await this.getOrCreate();
        return settings[_serversettingsconstants.serverSettingsKey];
    }
    async patchServerSettings(serverSettingsPartial) {
        const entity = await this.getOrCreate();
        if (!entity[_serversettingsconstants.serverSettingsKey]) {
            throw new Error("No existing server settings found, cant patch");
        }
        const newServerSettings = {
            ...entity[_serversettingsconstants.serverSettingsKey],
            ...serverSettingsPartial
        };
        return await this.updateServerSettings(newServerSettings);
    }
    async updateServerSettings(serverSettings) {
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.serverSettingsKey] = serverSettings;
        await this.update(entity.id, entity);
        return entity;
    }
    async updateCredentialSettings(credentialSettings) {
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.credentialSettingsKey] = credentialSettings;
        await this.update(entity.id, entity);
        return entity;
    }
    async updateFileCleanSettings(fileCleanSettings) {
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.printerFileCleanSettingKey] = fileCleanSettings;
        await this.update(entity.id, entity);
        return entity;
    }
    async updateFrontendSettings(frontendSettings) {
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.frontendSettingKey] = frontendSettings;
        await this.update(entity.id, entity);
        return entity;
    }
    async updateTimeoutSettings(timeoutSettings) {
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.timeoutSettingKey] = timeoutSettings;
        await this.update(entity.id, entity);
        return entity;
    }
    async patchWizardSettings(wizardSettingsPartial) {
        const entity = await this.getOrCreate();
        if (!entity[_serversettingsconstants.wizardSettingKey]) {
            throw new Error("No existing wizard settings found, cant patch");
        }
        const newWizardSettings = {
            ...entity[_serversettingsconstants.wizardSettingKey],
            ...wizardSettingsPartial
        };
        return await this.updateWizardSettings(newWizardSettings);
    }
    async updateWizardSettings(wizardSettings) {
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.wizardSettingKey] = wizardSettings;
        await this.update(entity.id, entity);
        return entity;
    }
    async get() {
        const settingsList = await this.repository.find({
            take: 1
        });
        return settingsList?.length ? settingsList[0] : null;
    }
    migrateSettingsRuntime(knownSettings) {
        const entity = knownSettings;
        if (!entity[_serversettingsconstants.printerFileCleanSettingKey]) {
            entity[_serversettingsconstants.printerFileCleanSettingKey] = (0, _serversettingsconstants.getDefaultFileCleanSettings)();
        } else {
            entity[_serversettingsconstants.printerFileCleanSettingKey] = {
                autoRemoveOldFilesBeforeUpload: entity[_serversettingsconstants.printerFileCleanSettingKey].autoRemoveOldFilesBeforeUpload,
                autoRemoveOldFilesAtBoot: entity[_serversettingsconstants.printerFileCleanSettingKey].autoRemoveOldFilesBeforeUpload,
                autoRemoveOldFilesCriteriumDays: entity[_serversettingsconstants.printerFileCleanSettingKey].autoRemoveOldFilesCriteriumDays
            };
        }
        if (!entity[_serversettingsconstants.wizardSettingKey]) {
            entity[_serversettingsconstants.wizardSettingKey] = (0, _serversettingsconstants.getDefaultWizardSettings)();
        }
        if (!entity[_serversettingsconstants.timeoutSettingKey]) {
            entity[_serversettingsconstants.timeoutSettingKey] = (0, _serversettingsconstants.getDefaultTimeout)();
        }
        if (!entity[_serversettingsconstants.serverSettingsKey]) {
            entity[_serversettingsconstants.serverSettingsKey] = (0, _serversettingsconstants.getDefaultServerSettings)();
        } else {
            entity[_serversettingsconstants.serverSettingsKey] = {
                debugSettings: entity[_serversettingsconstants.serverSettingsKey].debugSettings,
                loginRequired: entity[_serversettingsconstants.serverSettingsKey].loginRequired,
                registration: entity[_serversettingsconstants.serverSettingsKey].registration,
                experimentalClientSupport: entity[_serversettingsconstants.serverSettingsKey].experimentalClientSupport,
                experimentalMoonrakerSupport: entity[_serversettingsconstants.serverSettingsKey].experimentalMoonrakerSupport,
                sentryDiagnosticsEnabled: entity[_serversettingsconstants.serverSettingsKey].sentryDiagnosticsEnabled
            };
        }
        if (!entity[_serversettingsconstants.credentialSettingsKey]) {
            entity[_serversettingsconstants.credentialSettingsKey] = (0, _serversettingsconstants.getDefaultCredentialSettings)();
        }
        if (!entity[_serversettingsconstants.frontendSettingKey]) {
            entity[_serversettingsconstants.frontendSettingKey] = (0, _serversettingsconstants.getDefaultFrontendSettings)();
        }
        return entity;
    }
    async patchCredentialSettings(patchUpdate) {
        const entity = await this.getOrCreate();
        if (!entity[_serversettingsconstants.credentialSettingsKey]) {
            throw new Error("No existing wizard settings found, cant patch");
        }
        const newCredentialSettings = {
            ...entity[_serversettingsconstants.credentialSettingsKey],
            ...patchUpdate
        };
        return await this.updateCredentialSettings(newCredentialSettings);
    }
    async patchFileCleanSettings(patchUpdate) {
        const entity = await this.getOrCreate();
        if (!entity[_serversettingsconstants.printerFileCleanSettingKey]) {
            throw new Error("No existing wizard settings found, cant patch");
        }
        const newFileCleanSettings = {
            ...entity[_serversettingsconstants.printerFileCleanSettingKey],
            ...patchUpdate
        };
        return await this.updateFileCleanSettings(newFileCleanSettings);
    }
    async setLoginRequired(enabled) {
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.serverSettingsKey].loginRequired = enabled;
        await this.update(entity.id, entity);
        return entity;
    }
    async setRegistrationEnabled(enabled) {
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.serverSettingsKey].registration = enabled;
        await this.update(entity.id, entity);
        return entity;
    }
    async setSentryDiagnosticsEnabled(enabled) {
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.serverSettingsKey].sentryDiagnosticsEnabled = enabled;
        await this.update(entity.id, entity);
        return entity;
    }
}

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