"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractInterceptor = void 0;
const rest_1 = require("@loopback/rest");
const lodash_1 = require("lodash");
const locales_helper_1 = require("./../helper/locales-helper");
const entity_name_1 = require("./../models/entity-name");
class AbstractInterceptor {
    async getUriParts(invocationCtx, ctx) {
        ctx.uriContext.mainContext = true;
        return ctx.uriContext.uri.getOrSetValue(async () => {
            const httpReq = (await invocationCtx.get(rest_1.RestBindings.Http.REQUEST, {
                optional: true,
            }));
            // httpReq.headers.host  ='127.0.0.1:3000'
            const host = httpReq.headers.host;
            const protocol = httpReq.headers.protocol
                ? httpReq.headers.protocol
                : httpReq.headers.origin
                    ? httpReq.headers.origin.split(':')[0]
                    : 'http';
            const baseUrl = httpReq === null || httpReq === void 0 ? void 0 : httpReq.baseUrl;
            const path = httpReq === null || httpReq === void 0 ? void 0 : httpReq.path;
            const baseUri = protocol + '://' + host + baseUrl;
            const objectUri = path + ((path === null || path === void 0 ? void 0 : path.endsWith('/')) ? '' : '/');
            const acceptLanguages = httpReq.acceptsLanguages();
            const acceptLanguage = locales_helper_1.LocalesHelper.getValidLocales(acceptLanguages ? acceptLanguages[0] : undefined);
            return {
                host: host,
                protocol: protocol,
                baseUri: baseUri,
                path: path,
                objectUri: objectUri,
                url: baseUri + objectUri,
                method: httpReq.method,
                headers: httpReq.headers,
                acceptLanguages: acceptLanguages,
                acceptLanguage: acceptLanguage,
            };
        });
    }
    getEntityName(objectUri, baseUri) {
        const relativeObjectUri = objectUri.replace(baseUri, '');
        const entityUri = relativeObjectUri
            .substr('/' === relativeObjectUri.charAt(0) ? 1 : 0)
            .split('/')[0];
        const entityCamelCase = lodash_1.camelCase(entityUri.substr(0, entityUri.length - 1));
        return entity_name_1.EntityName[entityCamelCase];
    }
}
exports.AbstractInterceptor = AbstractInterceptor;
//# sourceMappingURL=abstract.interceptor.js.map