"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SettingsService", {
    enumerable: true,
    get: function() {
        return SettingsService;
    }
});
const _entities = require("../../entities");
const _serversettingsconstants = require("../../constants/server-settings.constants");
const _baseservice = require("./base.service");
const _settingsdto = require("../interfaces/settings.dto");
const _settingsservicevalidation = require("../validators/settings-service.validation");
const _runtimesettingsmigration = require("../../shared/runtime-settings.migration");
const _validators = require("../../handlers/validators");
class SettingsService extends (0, _baseservice.BaseService)(_entities.Settings, _settingsdto.SettingsDto) {
    toDto(entity) {
        return {
            [_serversettingsconstants.serverSettingsKey]: entity[_serversettingsconstants.serverSettingsKey],
            [_serversettingsconstants.frontendSettingKey]: entity[_serversettingsconstants.frontendSettingKey],
            [_serversettingsconstants.wizardSettingKey]: entity[_serversettingsconstants.wizardSettingKey],
            [_serversettingsconstants.timeoutSettingKey]: entity[_serversettingsconstants.timeoutSettingKey]
        };
    }
    async getOrCreate() {
        let settings = await this.getOptional();
        if (settings) {
            settings = (0, _runtimesettingsmigration.migrateSettingsRuntime)(settings);
            const settingsId = settings.id;
            return await this.update(settingsId, settings);
        } else {
            return await this.create((0, _serversettingsconstants.getDefaultSettings)());
        }
    }
    async updateServerSettings(update) {
        const validatedInput = await (0, _validators.validateInput)(update, _settingsservicevalidation.serverSettingsUpdateSchema);
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.serverSettingsKey] = validatedInput;
        await this.update(entity.id, entity);
        return entity;
    }
    async updateJwtSecretCredentialSetting(update) {
        const validatedInput = await (0, _validators.validateInput)(update, _settingsservicevalidation.jwtSecretCredentialSettingUpdateSchema);
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.credentialSettingsKey].jwtSecret = validatedInput.jwtSecret;
        await this.update(entity.id, entity);
        return entity;
    }
    async updateCoreCredentialSettings(update) {
        const validatedInput = await (0, _validators.validateInput)(update, _settingsservicevalidation.credentialCoreSettingUpdateSchema);
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.credentialSettingsKey].refreshTokenExpiry = validatedInput.refreshTokenExpiry;
        entity[_serversettingsconstants.credentialSettingsKey].refreshTokenAttempts = validatedInput.refreshTokenAttempts;
        entity[_serversettingsconstants.credentialSettingsKey].jwtExpiresIn = validatedInput.jwtExpiresIn;
        await this.update(entity.id, entity);
        return entity;
    }
    async updateSlicerApiKey(update) {
        const validatedInput = await (0, _validators.validateInput)(update, _settingsservicevalidation.slicerApiKeyUpdateSchema);
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.credentialSettingsKey].slicerApiKey = validatedInput.slicerApiKey;
        await this.update(entity.id, entity);
        return entity;
    }
    async updateFrontendSettings(update) {
        const validatedInput = await (0, _validators.validateInput)(update, _settingsservicevalidation.frontendSettingsUpdateSchema);
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.frontendSettingKey] = validatedInput;
        await this.update(entity.id, entity);
        return entity;
    }
    async updateTimeoutSettings(update) {
        const validatedInput = await (0, _validators.validateInput)(update, _settingsservicevalidation.timeoutSettingsUpdateSchema);
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.timeoutSettingKey] = validatedInput;
        await this.update(entity.id, entity);
        return entity;
    }
    async updateWizardSettings(update) {
        const validatedInput = await (0, _validators.validateInput)(update, _settingsservicevalidation.wizardUpdateSchema);
        const entity = await this.getOrCreate();
        entity[_serversettingsconstants.wizardSettingKey] = validatedInput;
        await this.update(entity.id, entity);
        return entity;
    }
    async getOptional() {
        const settingsList = await this.repository.find({
            take: 1
        });
        return settingsList?.length ? settingsList[0] : null;
    }
}

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