"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceClientImpl = void 0;
const HttpMethodType_1 = require("./enums/HttpMethodType");
const RequestBodyType_1 = require("./enums/RequestBodyType");
const SimpleHttpUtil_1 = require("./utils/http/SimpleHttpUtil");
const GatewayConfig_1 = require("./config/GatewayConfig");
const UuidUtil_1 = require("./utils/UuidUtil");
const MsConsts_1 = require("./defines/MsConsts");
const CookieUtils_1 = require("./utils/CookieUtils");
/**
 * 微服务客户端使用接口
 * @author WuFenglin
 **/
class ServiceClientImpl {
    constructor(serviceClientConfig) {
        this.gatewayConfig = new GatewayConfig_1.GatewayConfig();
        this.serviceClientConfig = serviceClientConfig;
        this.simpleHttpUtil = new SimpleHttpUtil_1.SimpleHttpUtil(this.serviceClientConfig);
    }
    call(httpMethod, headers, params, bodyObj, bodyType) {
        return __awaiter(this, void 0, void 0, function* () {
            let requestBodyObj = bodyObj;
            if (bodyType == RequestBodyType_1.RequestBodyType.WWW_FORM) {
                requestBodyObj = params;
            }
            return this.sendRequest(httpMethod, headers, params, requestBodyObj, bodyType);
        });
    }
    get(headers, params) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendRequest(HttpMethodType_1.HttpMethodType.GET, headers, params, {}, RequestBodyType_1.RequestBodyType.WWW_FORM);
        });
    }
    upload(headers, params, formData) {
        return __awaiter(this, void 0, void 0, function* () {
            let bodyObj = params || {};
            return this.sendRequest(HttpMethodType_1.HttpMethodType.UPLOAD, headers, params, bodyObj, RequestBodyType_1.RequestBodyType.WWW_FORM, formData);
        });
    }
    sendRequest(method, headers, params, bodyObj, bodyType, formData) {
        return __awaiter(this, void 0, void 0, function* () {
            let result;
            const gatewayUrl = this.gatewayConfig.getHostAddress();
            this.processHeaders(headers);
            switch (method) {
                case HttpMethodType_1.HttpMethodType.UPLOAD:
                    if (formData != null) {
                        result = yield this.simpleHttpUtil.upload(gatewayUrl, params, formData, headers);
                    }
                    else {
                        throw new Error('Upload Error files is not empty...');
                    }
                    break;
                default:
                    result = yield this.simpleHttpUtil.request(method, gatewayUrl, params, bodyObj, headers, bodyType);
                    break;
            }
            return result.data;
        });
    }
    processHeaders(headers) {
        let parentCallId = "";
        let uuid = UuidUtil_1.default.get();
        headers[MsConsts_1.MsConsts.MS_CLIENT_VERSION_KEY] = MsConsts_1.MsConsts.MS_CLIENT_VERSION;
        headers[MsConsts_1.MsConsts.USER_DOMAIN_KEY] = CookieUtils_1.default.get(MsConsts_1.MsConsts.USER_TOKEN_KEY);
        ;
        headers[MsConsts_1.MsConsts.USER_TOKEN_KEY] = CookieUtils_1.default.get(MsConsts_1.MsConsts.USER_TOKEN_KEY);
        headers[MsConsts_1.MsConsts.PARENT_CALL_ID_KEY] = parentCallId;
        headers[MsConsts_1.MsConsts.TRACE_ID_KEY] = uuid;
        headers[MsConsts_1.MsConsts.CALL_ID_KEY] = uuid;
        headers[MsConsts_1.MsConsts.SPAN_ID_KEY] = uuid;
    }
}
exports.ServiceClientImpl = ServiceClientImpl;
//# sourceMappingURL=ServiceClientImpl.js.map