"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractContentProvider = void 0;
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
const merge_1 = __importDefault(require("lodash/merge"));
const url_join_1 = __importDefault(require("url-join"));
const config_1 = require("../config");
class AbstractContentProvider {
    constructor(session) {
        this.session = session;
    }
    getEndpointPath(pathName) {
        const configEndpoint = this.getEndpointConfig();
        if (!configEndpoint) {
            throw new Error(`Configuration for the endpoint '${pathName}' is missed.`);
        }
        return (0, url_join_1.default)(configEndpoint.url, configEndpoint.endpoints[pathName]);
    }
    getEndpointConfig() {
        const allGroup = config_1.config.get('apis')[this.apiGroup];
        const currentEndpointConfig = allGroup === null || allGroup === void 0 ? void 0 : allGroup[this.endpointName];
        const sessionOverriddenEndpoints = this.session.getOverriddenEndpoint(this.apiGroup, this.endpointName);
        if (!(currentEndpointConfig || sessionOverriddenEndpoints)) {
            return void 0;
        }
        return (0, merge_1.default)((0, cloneDeep_1.default)(currentEndpointConfig), sessionOverriddenEndpoints);
    }
    toContentResponse(endpointResponse, buildTable) {
        const formattedResponse = Object.assign(Object.assign({}, endpointResponse), { data: {
                raw: endpointResponse.data,
            } });
        let responseTable;
        // lazy 'table' property initialization with data caching
        if (buildTable) {
            Object.defineProperty(formattedResponse.data, 'table', {
                get: () => {
                    if (responseTable !== undefined) {
                        return responseTable;
                    }
                    responseTable = buildTable();
                    return responseTable;
                },
            });
        }
        return formattedResponse;
    }
}
exports.AbstractContentProvider = AbstractContentProvider;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWJzdHJhY3QtY29udGVudC1wcm92aWRlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9jb250ZW50L2Fic3RyYWN0LWNvbnRlbnQtcHJvdmlkZXIudHMiXSwibmFtZXMiO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