"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectTreeDefinitionService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const helper_1 = require("../../helper");
const entity_name_1 = require("../../models/entity-name");
const application_service_1 = require("../application.service");
const object_type_service_1 = require("../object-type.service");
const application_service_2 = require("./../application.service");
const object_node_service_1 = require("./../object-node/object-node.service");
const entity_definition_cont_1 = require("./entity-definition.cont");
const entity_definition_service_1 = require("./entity-definition.service");
const mustache_service_1 = require("./mustache.service");
const object_node_definition_service_1 = require("./object-node-definition.service");
let ObjectTreeDefinitionService = class ObjectTreeDefinitionService {
    constructor(objectTypeService, objectNodeService, objectNodeDefinitionService, entityDefinitionService, mustacheService) {
        this.objectTypeService = objectTypeService;
        this.objectNodeService = objectNodeService;
        this.objectNodeDefinitionService = objectNodeDefinitionService;
        this.entityDefinitionService = entityDefinitionService;
        this.mustacheService = mustacheService;
        this.providerId = entity_definition_cont_1.ENTITY_DEFINITION_PROVIDER;
        this.serviceId = ObjectTreeDefinitionService.name;
        this.entityDefinitionService.registerEntityDefinitionService(entity_name_1.EntityName.objectTree, this);
    }
    async completeReturnedEntity(entity, ctx) {
        var _a, _b, _c, _d;
        const objectTree = entity;
        //this.entityCtx?.entityDefinition
        if (!objectTree.entityCtx) {
            objectTree.entityCtx = {
                entityType: entity_name_1.EntityName.objectTree,
            };
        }
        if (!objectTree.entityCtx.preview) {
            objectTree.entityCtx.preview = {
                icon: '',
                html: `<span class="child-tree-preview">{{dataNode.title}}</span>`,
            };
        }
        const treeType = await ctx.treeContext.treeType.getOrSetValue(async () => {
            return this.objectTypeService.searchById(objectTree.treeNode.objectTypeId);
        });
        if (objectTree.entityCtx.preview &&
            treeType.iconView &&
            '' !== treeType.iconView) {
            objectTree.entityCtx.preview.icon = treeType.iconView;
        }
        if (objectTree.entityCtx.preview &&
            treeType.title &&
            '' !== treeType.title) {
            objectTree.entityCtx.preview.typeTitle = treeType.title;
        }
        else {
            objectTree.entityCtx.preview.typeTitle = helper_1.toStartCase(objectTree.treeNode.objectTypeId);
        }
        if (treeType.templateView && '' !== treeType.templateView) {
            objectTree.entityCtx.preview.html = treeType.templateView;
        }
        objectTree.entityCtx.preview.html = this.mustacheService.parse(objectTree.entityCtx.preview.html, {
            dataTree: objectTree,
            dataNode: objectTree.treeNode,
        });
        objectTree.entityCtx.implementedTypes = (_a = treeType.entityCtx) === null || _a === void 0 ? void 0 : _a.implementedTypes;
        if (!objectTree.entityCtx.actions) {
            objectTree.entityCtx.actions = {};
        }
        if (!objectTree.entityCtx.actions.creations) {
            objectTree.entityCtx.actions.creations = {};
        }
        const childContext = new application_service_2.NodeContext();
        childContext.brothers.value = ctx.treeContext.treeChildren.value;
        childContext.parent.value = objectTree.treeNode;
        childContext.parentType.value = treeType;
        if (treeType.objectSubTypes && ((_d = (_c = (_b = objectTree.entityCtx) === null || _b === void 0 ? void 0 : _b.aclCtx) === null || _c === void 0 ? void 0 : _c.rights) === null || _d === void 0 ? void 0 : _d.create)) {
            for (const subType of treeType.objectSubTypes) {
                if (objectTree.childrenEntityCtx &&
                    subType.subObjectTypeId in objectTree.childrenEntityCtx &&
                    'create' in
                        objectTree.childrenEntityCtx[subType.subObjectTypeId].aclCtx
                            .rights &&
                    !objectTree.childrenEntityCtx[subType.subObjectTypeId].aclCtx.rights
                        .create) {
                    continue;
                }
                try {
                    childContext.objectSubType.value = subType;
                    await this.objectNodeService.checkBrothersCondition(entity.id, childContext);
                    objectTree.entityCtx.actions.creations[subType.subObjectTypeId] = {
                        schema: await this.objectNodeDefinitionService.getObjectNodeDefinition(await this.objectTypeService.searchById(subType.subObjectTypeId), childContext, true),
                    };
                    const objectType = await this.objectTypeService.searchById(subType.subObjectTypeId);
                    if (objectType.iconView) {
                        objectTree.entityCtx.actions.creations[subType.subObjectTypeId].icon = objectType.iconView;
                    }
                    if (objectType.title) {
                        objectTree.entityCtx.actions.creations[subType.subObjectTypeId].typeTitle = objectType.title;
                    }
                    else {
                        objectTree.entityCtx.actions.creations[subType.subObjectTypeId].typeTitle = helper_1.toStartCase(subType.subObjectTypeId);
                    }
                    // eslint-disable-next-line no-empty
                }
                catch (error) { }
            }
        }
        objectTree.entityCtx.actions.reads = [];
        const knownSubObjectTypeIds = treeType.objectSubTypes
            ? treeType.objectSubTypes.map((subType) => subType.subObjectTypeId)
            : [];
        for (let index = knownSubObjectTypeIds.length - 1; index >= 0; index--) {
            if (objectTree.childrenEntityCtx &&
                knownSubObjectTypeIds[index] in objectTree.childrenEntityCtx &&
                'read' in
                    objectTree.childrenEntityCtx[knownSubObjectTypeIds[index]].aclCtx
                        .rights &&
                !objectTree.childrenEntityCtx[knownSubObjectTypeIds[index]].aclCtx
                    .rights.read) {
                knownSubObjectTypeIds.splice(index, 1);
            }
        }
        if (objectTree.children) {
            for (let childIndex = objectTree.children.length - 1; childIndex >= 0; childIndex--) {
                const child = objectTree.children[childIndex];
                if (-1 ===
                    objectTree.entityCtx.actions.reads.indexOf(child.treeNode.objectTypeId) &&
                    -1 < knownSubObjectTypeIds.indexOf(child.treeNode.objectTypeId)) {
                    objectTree.entityCtx.actions.reads.push(child.treeNode.objectTypeId);
                }
                else if (-1 === knownSubObjectTypeIds.indexOf(child.treeNode.objectTypeId)) {
                    objectTree.children.splice(childIndex, 1);
                }
            }
        }
        for (const childCreationObjectTypeId of Object.keys(objectTree.entityCtx.actions.creations)) {
            if (-1 ===
                objectTree.entityCtx.actions.reads.indexOf(childCreationObjectTypeId)) {
                objectTree.entityCtx.actions.reads.push(childCreationObjectTypeId);
            }
        }
        for (const child of objectTree.children) {
            if (!child.treeNode.tree) {
                await this.entityDefinitionService.completeReturnedEntity(entity_name_1.EntityName.objectTree, child, application_service_1.CurrentContext.get(ctx, {}));
            }
        }
    }
};
ObjectTreeDefinitionService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(object_type_service_1.ObjectTypeService)),
    tslib_1.__param(1, core_1.service(object_node_service_1.ObjectNodeService)),
    tslib_1.__param(2, core_1.service(object_node_definition_service_1.ObjectNodeDefinitionService)),
    tslib_1.__param(3, core_1.service(entity_definition_service_1.EntityDefinitionService)),
    tslib_1.__param(4, core_1.service(mustache_service_1.MustacheService)),
    tslib_1.__metadata("design:paramtypes", [object_type_service_1.ObjectTypeService,
        object_node_service_1.ObjectNodeService,
        object_node_definition_service_1.ObjectNodeDefinitionService,
        entity_definition_service_1.EntityDefinitionService,
        mustache_service_1.MustacheService])
], ObjectTreeDefinitionService);
exports.ObjectTreeDefinitionService = ObjectTreeDefinitionService;
//# sourceMappingURL=object-tree-definition.service.js.map