"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerConfigService = void 0;
const tslib_1 = require("tslib");
/* eslint-disable @typescript-eslint/no-explicit-any */
const core_1 = require("@loopback/core");
const models_1 = require("../../models");
const services_1 = require("../../services");
const transient_entity_service_1 = require("./../../services/transient-entity/transient-entity.service");
const content_encrypted_object_definition_1 = require("./../content-encrypted-object/content-encrypted-object.definition");
const server_config_const_1 = require("./../server-config/server-config.const");
let ServerConfigService = class ServerConfigService {
    constructor(appCtx, transientEntityService, objectNodeService, contentEncryptedObjectService) {
        this.appCtx = appCtx;
        this.transientEntityService = transientEntityService;
        this.objectNodeService = objectNodeService;
        this.contentEncryptedObjectService = contentEncryptedObjectService;
        this.properties = {};
        this.transientEntityService.registerTransientEntityTypeFunction(server_config_const_1.SERVER_CONFIG_PROVIDER, ServerConfigService.name, 'Add server config proprties definition', models_1.EntityName.objectNode, server_config_const_1.SERVER_CONFIGURATION_TYPE.name, this.completeServerConfigurationNode.bind(this));
    }
    async completeServerConfigurationNode(objectNode, ctx) {
        var _a, _b, _c, _d, _e;
        if ((_e = (_d = (_c = (_b = (_a = objectNode.entityCtx) === null || _a === void 0 ? void 0 : _a.jsonSchema) === null || _b === void 0 ? void 0 : _b.properties) === null || _c === void 0 ? void 0 : _c.contentEncryptedObject) === null || _d === void 0 ? void 0 : _d.properties) === null || _e === void 0 ? void 0 : _e.value) {
            objectNode.entityCtx.jsonSchema.properties.contentEncryptedObject.properties.value.properties = this.properties;
            delete objectNode.entityCtx.jsonSchema.properties.contentEncryptedObject
                .properties.value['x-schema-form'];
        }
    }
    async getConfig() {
        const config = this.appCtx.getExtensionContext(server_config_const_1.SERVER_CONFIG_PROVIDER).nodes
            .serverConfiguration.value;
        if (!config) {
            return {};
        }
        const configObj = await this.objectNodeService.searchById(config.id);
        await this.contentEncryptedObjectService.addTransientContent(configObj);
        if ((configObj === null || configObj === void 0 ? void 0 : configObj.contentEncryptedObject) &&
            'value' in configObj.contentEncryptedObject) {
            return configObj.contentEncryptedObject.value;
        }
        return {};
    }
    async setConfig(configValue) {
        const config = this.appCtx.getExtensionContext(server_config_const_1.SERVER_CONFIG_PROVIDER).nodes
            .serverConfiguration.value;
        if (!config) {
            return;
        }
        const configObj = await this.objectNodeService.searchById(config.id);
        await this.contentEncryptedObjectService.manageContent(configObj, {
            contentEncryptedObject: { value: configValue },
        });
    }
    async get(key, defaultValue) {
        const config = await this.getConfig();
        if (config && key in config) {
            return config[key];
        }
        if (undefined !== defaultValue) {
            config[key] = await defaultValue();
            await this.setConfig(config);
            return config[key];
        }
        return undefined;
    }
    registerNewConfigurationKey(key, type) {
        this.properties[key] = type;
    }
};
ServerConfigService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(services_1.ApplicationService)),
    tslib_1.__param(1, core_1.service(transient_entity_service_1.TransientEntityService)),
    tslib_1.__param(2, core_1.service(services_1.ObjectNodeService)),
    tslib_1.__param(3, core_1.service(content_encrypted_object_definition_1.ContentEncryptedObjectService)),
    tslib_1.__metadata("design:paramtypes", [services_1.ApplicationService,
        transient_entity_service_1.TransientEntityService,
        services_1.ObjectNodeService,
        content_encrypted_object_definition_1.ContentEncryptedObjectService])
], ServerConfigService);
exports.ServerConfigService = ServerConfigService;
//# sourceMappingURL=server-config.service.js.map