"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RootConfigService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const entity_name_1 = require("../../models/entity-name");
const application_service_1 = require("../application.service");
const transient_entity_service_1 = require("../transient-entity/transient-entity.service");
const root_config_const_1 = require("./root-config.const");
let RootConfigService = class RootConfigService {
    constructor(transientEntityService, appCtx) {
        this.transientEntityService = transientEntityService;
        this.appCtx = appCtx;
        this.transientEntityService.registerTransientEntityTypeFunction(root_config_const_1.ROOT_CONFIG_PROVIDER, RootConfigService.name, 'Add Object Tree Configuration Summary', entity_name_1.EntityName.objectNode, root_config_const_1.ROOT_CONFIGURATION_TYPE.name, this.completeRootConfigurationNode.bind(this));
        this.transientEntityService.registerTransientEntityTypeFunction(root_config_const_1.ROOT_CONFIG_PROVIDER, RootConfigService.name, 'Change access rights of Object Tree Configuration Summary', entity_name_1.EntityName.objectTree, root_config_const_1.ROOT_CONFIGURATION_TYPE.name, this.completeRootConfigurationTree.bind(this));
    }
    async completeRootConfigurationTree(object, ctx) {
        if (object.entityCtx) {
            if (object.entityCtx.aclCtx) {
                object.entityCtx.aclCtx.rights.update = false;
                object.entityCtx.aclCtx.rights.delete = false;
            }
        }
    }
    async completeRootConfigurationNode(object, ctx) {
        var _a;
        if (object.entityCtx) {
            if (object.entityCtx.aclCtx) {
                object.entityCtx.aclCtx.rights.update = false;
                object.entityCtx.aclCtx.rights.delete = false;
            }
            if ((_a = object.entityCtx.jsonSchema) === null || _a === void 0 ? void 0 : _a.properties) {
                for (const key of [
                    'rss',
                    'heapTotal',
                    'heapUsed',
                    'external',
                    'cumulated',
                ]) {
                    object.entityCtx.jsonSchema.properties[key] = {
                        type: 'string',
                        title: this.getMemoryKeyDefinition(key),
                        'x-schema-form': {
                            readonly: true,
                            disabled: true,
                        },
                    };
                }
                object.entityCtx.jsonSchema.properties.configSummary = {
                    type: 'string',
                    'x-schema-form': {
                        type: 'textarea',
                        readonly: true,
                        disabled: true,
                    },
                };
                const memory = process.memoryUsage();
                for (const key of ['rss', 'heapTotal', 'heapUsed', 'external']) {
                    object[key] = `${Math.round((memory[key] / 1024 / 1024) * 100) / 100} MB`;
                }
                object.cumulated = `${Math.round(((memory.rss + memory.external) / 1024 / 1024) * 100) / 100} MB`;
                object.configSummary = this.appCtx.configSummary.descriptionLines.join('\r\n');
            }
        }
    }
    getMemoryKeyDefinition(key) {
        return {
            rss: 'Total memory allocated',
            heapTotal: 'Total size of the allocated heap',
            heapUsed: 'Actual memory used',
            external: 'External memory usage',
            cumulated: 'Total allocated and external',
        }[key];
    }
};
RootConfigService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(transient_entity_service_1.TransientEntityService)),
    tslib_1.__param(1, core_1.service(application_service_1.ApplicationService)),
    tslib_1.__metadata("design:paramtypes", [transient_entity_service_1.TransientEntityService,
        application_service_1.ApplicationService])
], RootConfigService);
exports.RootConfigService = RootConfigService;
//# sourceMappingURL=root-config.service.js.map