"use strict";
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
var PROTOCOL_TO_PORT_MAPPING = (_a = {},
    _a["http:" /* EndpointProtocol.Http */] = 80,
    _a["https:" /* EndpointProtocol.Https */] = 443,
    _a["ws:" /* EndpointProtocol.WebSocket */] = 80,
    _a["wss:" /* EndpointProtocol.SecureWebSocket */] = 443,
    _a);
var PORT_TO_PROTOCOL_MAPPING = {
    80: "http:" /* EndpointProtocol.Http */,
    443: "https:" /* EndpointProtocol.Https */
};
var URLEndpoint = /** @class */ (function () {
    function URLEndpoint(protocol, host, port) {
        this.protocol = protocol;
        this.host = host;
        this.port = port || PROTOCOL_TO_PORT_MAPPING[protocol];
    }
    URLEndpoint.prototype.encode = function () {
        return "".concat(this.protocol.valueOf(), "//").concat(this.host, ":").concat(this.port);
    };
    URLEndpoint.fromProtocolString = function (protocol, port) {
        switch (protocol) {
            //@formatter:off
            case 'http:':
                return "http:" /* EndpointProtocol.Http */;
            case 'https:':
                return "https:" /* EndpointProtocol.Https */;
            case 'ws:':
                return "ws:" /* EndpointProtocol.WebSocket */;
            case 'wss:':
                return "wss:" /* EndpointProtocol.SecureWebSocket */;
            //@formatter:on
        }
        if (port && PORT_TO_PROTOCOL_MAPPING[port]) {
            return PORT_TO_PROTOCOL_MAPPING[port];
        }
        // fallback to HTTPS
        return "https:" /* EndpointProtocol.Https */;
    };
    URLEndpoint.parseHostAndPort = function (input) {
        var _a = __read(input.split('/'), 1), hostAndPort = _a[0];
        var _b = __read(hostAndPort.split(':'), 2), host = _b[0], port = _b[1];
        return { host: host, port: port };
    };
    URLEndpoint.parse = function (url) {
        var _a = __read(url.split('//'), 2), protocol = _a[0], otherUrlParts = _a[1];
        if (!otherUrlParts) {
            otherUrlParts = protocol;
            protocol = null;
        }
        var _b = this.parseHostAndPort(otherUrlParts), host = _b.host, port = _b.port;
        var parsesPort = port && parseInt(port);
        return new URLEndpoint(this.fromProtocolString(protocol, parsesPort), host, parsesPort);
    };
    return URLEndpoint;
}());
exports.default = URLEndpoint;
//# sourceMappingURL=URLEndpoint.js.map