"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiCrudClientBase = void 0;
const tslib_1 = require("tslib");
const inversify_1 = require("inversify");
const _ = require("lodash");
const api_client_base_1 = require("./api-client-base");
const codiac_common_1 = require("@codiac.io/codiac-common");
let ApiCrudClientBase = class ApiCrudClientBase extends api_client_base_1.ApiClientBase {
    constructor(logger, apiRootUrl, authToken) {
        super(logger, apiRootUrl, authToken);
        this.authToken = authToken;
    }
    /** TODO: Move this to ApiClientBase */
    headersFrom(options, existingHeaders) {
        let output;
        // FROM the api base...
        /** The name of the http header used to designate the csv list of properties to include in the output signature */
        const HEADER_KEY_PROJECTION_INCLUDES = "output-includes";
        /** The name of the http header used to designate the csv list of properties to include in the output signature */
        const HEADER_KEY_PROJECTION_EXCLUDES = "output-excludes";
        if (options === null || options === void 0 ? void 0 : options.projection) {
            output = Object.assign({}, (existingHeaders !== null && existingHeaders !== void 0 ? existingHeaders : {}));
            output[HEADER_KEY_PROJECTION_INCLUDES] = options.projection.pathsIncluded.join(",");
            output[HEADER_KEY_PROJECTION_EXCLUDES] = options.projection.pathsExcluded.join(",");
        }
        return output;
    }
    tryMakeRestCall(ep, request, options) {
        var _a, _b;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let response = { success: false };
            try {
                let url;
                if ([codiac_common_1.httpRestVerb.DELETE, codiac_common_1.httpRestVerb.GET].toString().includes(ep.verb)) {
                    let values = request;
                    if (ep.isMany) {
                        let serializerMap = {
                            "code": "StringFilter",
                            "name": "StringFilter"
                        };
                        values = this.applyPropertySerializers(request, serializerMap);
                    }
                    url = this.buildUrl(ep.route, values, true);
                }
                else {
                    url = this.buildUrl(ep.route, (ep.isMany || _.isArray(request)) ? {} : request, false);
                }
                let headers = options ? this.headersFrom(options) : undefined;
                let returnHeaders;
                [response, returnHeaders] = yield this.makeHttpCall(ep.verb, url, request, headers);
            }
            catch (err) {
                response.error = err;
                response.message = "Error performing ".concat(ep.op, " ", ep.entityTypeName, ": ", (_a = err.name) !== null && _a !== void 0 ? _a : "", (_b = err.message) !== null && _b !== void 0 ? _b : "");
            }
            finally {
                return response;
            }
        });
    }
    makeAnyRestCall(ep, request, options) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            try {
                let url = this.buildUrl(ep.route, request, [codiac_common_1.httpRestVerb.DELETE, codiac_common_1.httpRestVerb.GET].toString().includes(ep.verb));
                let headers = options ? this.headersFrom(options) : undefined;
                let [response, returnHeaders] = yield this.makeHttpCall(ep.verb, url, request, headers);
                return response;
            }
            catch (err) {
                throw err;
            }
        });
    }
}; // end class 
ApiCrudClientBase = tslib_1.__decorate([
    (0, inversify_1.injectable)(),
    tslib_1.__param(0, (0, inversify_1.inject)(codiac_common_1.INTERFACES.ILogger)),
    tslib_1.__param(1, (0, inversify_1.unmanaged)()),
    tslib_1.__param(2, (0, inversify_1.unmanaged)()),
    tslib_1.__metadata("design:paramtypes", [Object, String, String])
], ApiCrudClientBase);
exports.ApiCrudClientBase = ApiCrudClientBase;
//# sourceMappingURL=api-crud-client-base.js.map