"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CleanEntityInterceptor = exports.CleanEntityService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const lodash_1 = require("lodash");
const helper_1 = require("../../helper");
const abstract_entity_interceptor_1 = require("../../interceptors/abstract-entity.interceptor");
const entity_name_1 = require("./../../models/entity-name");
const application_service_1 = require("./../application.service");
const inside_rest_service_1 = require("./../inside-rest/inside-rest.service");
let CleanEntityService = class CleanEntityService {
    constructor(insideRestService) {
        this.insideRestService = insideRestService;
    }
    async completeReturnedEntity(entityName, entity, ctx) {
        helper_1.cancelRecursive(entity);
        if (lodash_1.isArray(entity.children)) {
            await this.completeReturnedEntities(entityName, entity.children, ctx);
        }
        if (lodash_1.isObject(entity.treeNode)) {
            await this.completeReturnedEntity(entity_name_1.EntityName.objectNode, entity.treeNode, ctx);
        }
        // CHANGE : used by move that use internal REST
        if (!this.insideRestService.isInternalCall(ctx)) {
            // remove entityCtx.implementedTypes
            if (entity.entityCtx) {
                delete entity.entityCtx.implementedTypes;
            }
            //remove entityName
            delete entity.entityName;
            // remove childrenEntityCtx
            delete entity.childrenEntityCtx;
            // remove owner tree namespace index and acl
            delete entity.owner;
            delete entity.tree;
            delete entity.namespace;
            delete entity.index;
            delete entity.acl;
        }
    }
    async completeReturnedEntities(entityName, entities, ctx) {
        for (const entity of entities) {
            await this.completeReturnedEntity(entityName, entity, ctx);
        }
    }
};
CleanEntityService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(inside_rest_service_1.InsideRestService)),
    tslib_1.__metadata("design:paramtypes", [inside_rest_service_1.InsideRestService])
], CleanEntityService);
exports.CleanEntityService = CleanEntityService;
let CleanEntityInterceptor = class CleanEntityInterceptor extends abstract_entity_interceptor_1.AbstractEntityInterceptor {
    constructor(cleanEntityService, ctx, applicationService) {
        super(cleanEntityService, ctx, applicationService);
        this.cleanEntityService = cleanEntityService;
        this.ctx = ctx;
        this.applicationService = applicationService;
    }
};
CleanEntityInterceptor = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(CleanEntityService)),
    tslib_1.__param(1, core_1.inject(application_service_1.CURRENT_CONTEXT)),
    tslib_1.__param(2, core_1.service(application_service_1.ApplicationService)),
    tslib_1.__metadata("design:paramtypes", [CleanEntityService,
        application_service_1.CurrentContext,
        application_service_1.ApplicationService])
], CleanEntityInterceptor);
exports.CleanEntityInterceptor = CleanEntityInterceptor;
//# sourceMappingURL=clean-entity.service.js.map