"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransientTreeService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const models_1 = require("../../models");
const application_service_1 = require("../application.service");
const object_node_service_1 = require("./../object-node/object-node.service");
const transient_entity_const_1 = require("./transient-entity.const");
const transient_entity_service_1 = require("./transient-entity.service");
let TransientTreeService = class TransientTreeService {
    constructor(objectNodeService, transientEntityService) {
        this.objectNodeService = objectNodeService;
        this.transientEntityService = transientEntityService;
        this.providerId = transient_entity_const_1.TRANSIENT_ENTITY_PROVIDER;
        this.serviceId = TransientTreeService.name;
        this.description = 'Add owner, namespace, tree and aliasUri fields';
        this.transientEntityService.registerTransientEntityService(models_1.EntityName.objectTree, this);
    }
    async addOwnerNamespaceTreeAndAliasUriFields(objectTree, ctx) {
        if (objectTree.id && objectTree.uri) {
            if (objectTree.treeNode.owner) {
                objectTree.ownerType = objectTree.treeNode.objectTypeId;
                objectTree.ownerName = objectTree.treeNode.name;
                objectTree.aliasUri = objectTree.uri.replace(objectTree.id, 'owner/' + objectTree.ownerType + '/' + objectTree.ownerName);
            }
            else if (objectTree.treeNode.namespace) {
                const owner = await ctx.nodeContext.owner.getOrSetValue(async () => {
                    return this.objectNodeService.searchById(objectTree.treeNode.parentOwnerId);
                });
                objectTree.ownerType = owner.objectTypeId;
                objectTree.ownerName = owner.name;
                objectTree.namespaceType = objectTree.treeNode.objectTypeId;
                objectTree.namespaceName = objectTree.treeNode.name;
                objectTree.aliasUri = objectTree.uri.replace(objectTree.id, 'namespace/' +
                    objectTree.ownerType +
                    '/' +
                    objectTree.ownerName +
                    '/' +
                    objectTree.namespaceType +
                    '/' +
                    objectTree.namespaceName);
            }
            else if (objectTree.treeNode.tree) {
                const owner = await ctx.nodeContext.owner.getOrSetValue(async () => {
                    return this.objectNodeService.searchById(objectTree.treeNode.parentOwnerId);
                });
                const namespace = await ctx.nodeContext.namespace.getOrSetValue(async () => {
                    return this.objectNodeService.searchById(objectTree.treeNode.parentNamespaceId);
                });
                objectTree.ownerType = owner.objectTypeId;
                objectTree.ownerName = owner.name;
                objectTree.namespaceType = namespace.objectTypeId;
                objectTree.namespaceName = namespace.name;
                objectTree.treeType = objectTree.treeNode.objectTypeId;
                objectTree.treeName = objectTree.treeNode.name;
                objectTree.aliasUri = objectTree.uri.replace(objectTree.id, 'tree/' +
                    objectTree.ownerType +
                    '/' +
                    objectTree.ownerName +
                    '/' +
                    objectTree.namespaceType +
                    '/' +
                    objectTree.namespaceName +
                    '/' +
                    objectTree.treeType +
                    '/' +
                    objectTree.treeName);
            }
            else {
                objectTree.nodeName = objectTree.treeNode.name;
                objectTree.nodeType = objectTree.treeNode.objectTypeId;
            }
        }
    }
    async completeReturnedEntity(entity, ctx) {
        var _a;
        const objectTree = entity;
        await this.addOwnerNamespaceTreeAndAliasUriFields(objectTree, ctx);
        if (!objectTree.treeNode.entityCtx) {
            objectTree.treeNode.entityCtx = { entityType: models_1.EntityName.objectNode };
        }
        objectTree.treeNode.entityCtx.loaded = false;
        for (const child of objectTree.children) {
            if (!child.treeNode.tree) {
                await this.transientEntityService.completeReturnedEntity(models_1.EntityName.objectTree, child, application_service_1.CurrentContext.get(ctx, {}));
            }
            else {
                if (!child.entityCtx) {
                    child.entityCtx = { entityType: models_1.EntityName.objectTree };
                }
                child.entityCtx.loaded = false;
                if ((_a = child.treeNode) === null || _a === void 0 ? void 0 : _a.entityCtx) {
                    child.treeNode.entityCtx.loaded = false;
                }
            }
        }
    }
};
TransientTreeService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(object_node_service_1.ObjectNodeService)),
    tslib_1.__param(1, core_1.service(transient_entity_service_1.TransientEntityService)),
    tslib_1.__metadata("design:paramtypes", [object_node_service_1.ObjectNodeService,
        transient_entity_service_1.TransientEntityService])
], TransientTreeService);
exports.TransientTreeService = TransientTreeService;
//# sourceMappingURL=transient-tree.service.js.map