"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const MsNodeType_1 = require("../enums/MsNodeType");
const MsLocationType_1 = require("../enums/MsLocationType");
const CommUtils_1 = require("../utils/CommUtils");
/**
 * 调用链
 */
class CallLink {
    constructor(fromNode) {
        this.fromNode = fromNode;
    }
    build(uri, target) {
        const callTarget = new CallTarget();
        if (!uri.startsWith("/")) {
            uri = "/" + uri;
        }
        if (uri.endsWith("/")) {
            uri = uri.substring(0, uri.length - 1);
        }
        callTarget.host = target.host;
        callTarget.port = target.port;
        callTarget.contextPath = target.contextPath;
        //服务提供者在云端内网（非k8s集群内）--手动部署模式
        if (CommUtils_1.CommUtils.isEmpty(target.msLocation) || MsLocationType_1.MsLocationType.InsideNet === target.msLocation) {
            return this.updateCallType(callTarget, MsNodeType_1.MsNodeType.SERVICE);
        }
        //服务提供者在k8sCluster内
        if (MsLocationType_1.MsLocationType.K8sCluster === target.msLocation) {
            if (CommUtils_1.CommUtils.isEmpty(target.gatewayUrl) || CommUtils_1.CommUtils.isEmpty(target.gatewayInsideUrl)) {
                throw new Error(`MsRunningTarget: ${target.pid}/${target.projectVersion}, 'gatewayUrl' or 'gatewayInsideUrl' is not set! `);
            }
            //服务消费者和服务提供者在同一个集群内 -> 直接使用目标主机
            if (CommUtils_1.CommUtils.getGatewayUrl() && CommUtils_1.CommUtils.getGatewayUrl() === target.gatewayUrl) {
                return this.updateCallType(callTarget, MsNodeType_1.MsNodeType.K8S_SERVICE);
            }
            callTarget.setUrl(this.clientInInternet() ? target.gatewayUrl : target.gatewayInsideUrl);
            callTarget.contextPath = "";
            callTarget.uri = "/";
            return this.updateCallType(callTarget, MsNodeType_1.MsNodeType.K8S_GATEWAY);
        }
        throw new Error(`Service provider ${target.pid}, 'msLocation (${target.msLocation})' parameter is not configured correctly!`);
    }
    clientInInternet() {
        return !CommUtils_1.CommUtils.isK8SClient();
    }
    updateCallType(callTarget, to) {
        callTarget.from = this.fromNode;
        callTarget.to = to;
        return callTarget;
    }
}
exports.CallLink = CallLink;
class CallTarget {
    getUrl() {
        if (!this.port) {
            return `http://${this.host}${this.getFullUri()}`;
        }
        return `http://${this.host}:${this.port}${this.getFullUri()}`;
    }
    getFullUri() {
        let result = "";
        if (CommUtils_1.CommUtils.isNotEmpty(this.contextPath)) {
            result += "/" + this.contextPath;
        }
        if (!this.uri.startsWith("/")) {
            result += "/";
        }
        result += this.uri;
        if (this.params && CommUtils_1.CommUtils.isNotEmpty(this.params)) {
            result += ("?" + this.params);
        }
        return result;
    }
    setUrl(url) {
        if (!url || CommUtils_1.CommUtils.isEmpty(url)) {
            throw new Error('Gateway url is not  empty');
        }
        const tmp = CommUtils_1.CommUtils.rightStr(url, "//");
        const httpRoot = CommUtils_1.CommUtils.leftStrEx(tmp, "/");
        this.host = CommUtils_1.CommUtils.leftStrEx(httpRoot, ":");
        this.port = (url.indexOf(":") > -1) ? parseInt(CommUtils_1.CommUtils.rightStr(httpRoot, ":")) : 80;
        if (tmp.indexOf("/") > -1) {
            this.uri = "/" + CommUtils_1.CommUtils.rightStr(tmp, "/");
        }
        else {
            this.uri = "/";
        }
        if (url.indexOf("?") > -1) {
            this.params = CommUtils_1.CommUtils.rightStr(url, "?");
        }
        else {
            this.params = "";
        }
    }
}
exports.CallTarget = CallTarget;
