"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UriCompleteInterceptor = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const rest_1 = require("@loopback/rest");
const http_1 = require("http");
const uri_complete_service_1 = require("../services/uri-complete/uri-complete.service");
const application_service_1 = require("./../services/application.service");
const abstract_interceptor_1 = require("./abstract.interceptor");
/**
 * This class will be bound to the application as an `Interceptor` during
 * `boot`
 */
let UriCompleteInterceptor = class UriCompleteInterceptor extends abstract_interceptor_1.AbstractInterceptor {
    constructor(uriCompleteService, ctx) {
        super();
        this.uriCompleteService = uriCompleteService;
        this.ctx = ctx;
    }
    /**
     * This method is used by LoopBack context to produce an interceptor function
     * for the binding.
     *
     * @returns An interceptor function
     */
    value() {
        return this.intercept.bind(this);
    }
    /**
     * The logic to intercept an invocation
     * @param invocationCtx - Invocation context
     * @param next - A function to invoke next interceptor or the target method
     */
    async intercept(invocationCtx, next) {
        await this.getUriParts(invocationCtx, this.ctx);
        const result = await next();
        try {
            const httpReq = await invocationCtx.get(rest_1.RestBindings.Http.REQUEST, {
                optional: true,
            });
            const method = httpReq === null || httpReq === void 0 ? void 0 : httpReq.method;
            if (('GET' === method ||
                'POST' === method ||
                'PUT' === method ||
                'PATCH' === method) &&
                !(result instanceof http_1.ServerResponse)) {
                await this.getUriParts(invocationCtx, this.ctx);
                this.uriCompleteService.completeReturnedEntity(result, this.ctx);
            }
            // eslint-disable-next-line no-empty
        }
        catch (error) { }
        return result;
    }
};
UriCompleteInterceptor.BINDING_KEY = `interceptors.${UriCompleteInterceptor.name}`;
UriCompleteInterceptor = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(uri_complete_service_1.UriCompleteService)),
    tslib_1.__param(1, core_1.inject(application_service_1.CURRENT_CONTEXT)),
    tslib_1.__metadata("design:paramtypes", [uri_complete_service_1.UriCompleteService,
        application_service_1.CurrentContext])
], UriCompleteInterceptor);
exports.UriCompleteInterceptor = UriCompleteInterceptor;
//# sourceMappingURL=uri-complete.interceptor.js.map