"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeInterceptService = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const lodash_1 = require("lodash");
const extension_description_1 = require("../../integration/extension-description");
const entity_name_1 = require("./../../models/entity-name");
const object_node_service_1 = require("./../object-node/object-node.service");
const object_type_service_1 = require("./../object-type.service");
const entity_intercept_const_1 = require("./entity-intercept.const");
const entity_intercept_service_1 = require("./entity-intercept.service");
let NodeInterceptService = class NodeInterceptService {
    constructor(entityInterceptService, objectNodeService, objectTypeService) {
        this.entityInterceptService = entityInterceptService;
        this.objectNodeService = objectNodeService;
        this.objectTypeService = objectTypeService;
        this.interceptors = {};
    }
    registerEntityInterceptorService(functionProviderId, functionServiceId, functionDescription, objectType, scope, entityInterceptorFunction) {
        if (!(scope in this.interceptors)) {
            this.interceptors[scope] = new NodeIntercept(this.objectNodeService, scope, this.entityInterceptService, this.objectTypeService);
        }
        if (!(objectType in this.interceptors[scope].interceptors)) {
            this.interceptors[scope].interceptors[objectType] = [];
        }
        this.interceptors[scope].interceptors[objectType].push({
            functionDescription,
            functionProviderId,
            functionServiceId,
            entityInterceptorFunction,
        });
    }
};
NodeInterceptService = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(entity_intercept_service_1.EntityInterceptService)),
    tslib_1.__param(1, core_1.service(object_node_service_1.ObjectNodeService)),
    tslib_1.__param(2, core_1.service(object_type_service_1.ObjectTypeService)),
    tslib_1.__metadata("design:paramtypes", [entity_intercept_service_1.EntityInterceptService,
        object_node_service_1.ObjectNodeService,
        object_type_service_1.ObjectTypeService])
], NodeInterceptService);
exports.NodeInterceptService = NodeInterceptService;
class NodeIntercept {
    constructor(objectNodeService, scope, entityInterceptService, objectTypeService) {
        this.objectNodeService = objectNodeService;
        this.scope = scope;
        this.entityInterceptService = entityInterceptService;
        this.objectTypeService = objectTypeService;
        this.providerId = entity_intercept_const_1.ENTITY_INTERCEPT_PROVIDER;
        this.serviceId = NodeInterceptService.name;
        this.interceptors = {};
        this.entityInterceptService.registerEntityInterceptorService(entity_name_1.EntityName.objectNode, scope, this);
    }
    description() {
        const treatments = [];
        for (const objectType in this.interceptors) {
            const interceptors = this.interceptors[objectType];
            for (const interceptor of interceptors) {
                treatments.push(new extension_description_1.TreatmentDescription(interceptor.functionProviderId, interceptor.functionServiceId, objectType +
                    '.' +
                    this.scope +
                    ': ' +
                    interceptor.functionDescription));
            }
        }
        return treatments;
    }
    async interceptRequest(entityId, entity, ctx) {
        const requestEntity = entity;
        if (entityId) {
            entity = await this.objectNodeService.searchById(entityId);
        }
        if (!entity) {
            return true;
        }
        const entityObjectType = (await this.objectTypeService.getAll(ctx))[entity.objectTypeId];
        for (const objectType of Object.keys(this.interceptors)) {
            if (objectType === entityObjectType.id ||
                -1 < lodash_1.indexOf(entityObjectType.inheritedTypesIds, objectType)) {
                for (const interceptor of this.interceptors[objectType]) {
                    const result = await interceptor.entityInterceptorFunction(entityId, entity, requestEntity, ctx);
                    if (false === result) {
                        return false;
                    }
                    if (true !== result) {
                        return result;
                    }
                }
            }
        }
        return true;
    }
}
//# sourceMappingURL=node-intercept.service.js.map