"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityInterceptService = 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 application_service_1 = require("../application.service");
let EntityInterceptService = class EntityInterceptService {
    constructor(appCtx) {
        this.appCtx = appCtx;
        this.entityInterceptors = {};
        this.allInterceptors = [];
        this.finallyInterceptors = {};
        this.allFinalTreatments = [];
    }
    registerEntityInterceptorService(resource, scope, entityInterceptor) {
        var _a;
        if (!(resource in this.entityInterceptors)) {
            this.entityInterceptors[resource] = {};
        }
        if (!(scope in this.entityInterceptors[resource])) {
            this.entityInterceptors[resource][scope] = [];
        }
        (_a = this.entityInterceptors[resource][scope]) === null || _a === void 0 ? void 0 : _a.push(entityInterceptor);
    }
    registerAllInterceptorService(allInterceptor) {
        this.allInterceptors.push(allInterceptor);
    }
    registerFinallyInterceptorService(resource, scope, finallyInterceptor) {
        var _a;
        if (!(resource in this.finallyInterceptors)) {
            this.finallyInterceptors[resource] = {};
        }
        if (!(scope in this.finallyInterceptors[resource])) {
            this.finallyInterceptors[resource][scope] = [];
        }
        (_a = this.finallyInterceptors[resource][scope]) === null || _a === void 0 ? void 0 : _a.push(finallyInterceptor);
    }
    registerAllFinalTreatmentService(allFinalTreatment) {
        this.allFinalTreatments.push(allFinalTreatment);
    }
    async interceptRequest(entityName, scope, entityId, entity, ctx) {
        if (entityName in this.entityInterceptors &&
            scope in
                this.entityInterceptors[entityName]) {
            for (const interceptor of this.entityInterceptors[entityName][scope]) {
                const result = await interceptor.interceptRequest(entityId, entity, ctx);
                if (result === false) {
                    return false;
                }
                if (result !== true) {
                    return result;
                }
            }
        }
        return true;
    }
    async interceptAllRequest(ctx) {
        for (const intercept of this.allInterceptors) {
            await intercept.interceptRequest(ctx);
        }
    }
    async makeFinallyTreatment(entityName, scope, entityId, entity, ctx) {
        if (entityName in this.finallyInterceptors &&
            scope in
                this.finallyInterceptors[entityName]) {
            for (const finallyTreatment of this.finallyInterceptors[entityName][scope]) {
                await finallyTreatment.finallyRequest(entityId, entity, ctx);
            }
        }
        return;
    }
    async makeAllFinallyTreatment(ctx) {
        for (const intercept of this.allFinalTreatments) {
            await intercept.finallyRequest(ctx);
        }
    }
    getPreTreatmentDescription() {
        const treatments = [];
        for (const resource in this.entityInterceptors) {
            for (const scope in this.entityInterceptors[resource]) {
                for (const interceptor of this.entityInterceptors[resource][scope]) {
                    if (lodash_1.isString(interceptor.description)) {
                        treatments.push(new extension_description_1.TreatmentDescription(interceptor.providerId, interceptor.serviceId, resource + ': ' + interceptor.description));
                    }
                    else if (lodash_1.isFunction(interceptor.description)) {
                        const desc = interceptor.description();
                        if (lodash_1.isArray(desc)) {
                            treatments.push(...desc);
                        }
                        else {
                            treatments.push(desc);
                        }
                    }
                    else {
                        treatments.push(interceptor.description);
                    }
                }
            }
        }
        return treatments;
    }
};
EntityInterceptService = tslib_1.__decorate([
    core_1.injectable({ scope: core_1.BindingScope.SINGLETON }),
    tslib_1.__param(0, core_1.service(application_service_1.ApplicationService)),
    tslib_1.__metadata("design:paramtypes", [application_service_1.ApplicationService])
], EntityInterceptService);
exports.EntityInterceptService = EntityInterceptService;
//# sourceMappingURL=entity-intercept.service.js.map