text: "use strict";
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
      var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
      if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
      else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
      return c > 3 && r && Object.defineProperty(target, key, r), r;
  };
  var __importDefault = (this && this.__importDefault) || function (mod) {
      return (mod && mod.__esModule) ? mod : { "default": mod };
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  const url_1 = __importDefault(require("url"));
  const url_converter_1 = require("../util/url.converter");
  const inversify_1 = require("inversify");
  let UpstreamProxyManager = class UpstreamProxyManager {
      init(httpProxy, httpsProxy, noProxy) {
          if (httpProxy && this.validateUpstreamProxy(httpProxy, 'HTTP_PROXY')) {
              this._httpProxyUrl = url_converter_1.toCompleteUrl(httpProxy, false);
          }
          if (httpsProxy && this.validateUpstreamProxy(httpsProxy, 'HTTPS_PROXY')) {
              this._httpsProxyUrl = url_converter_1.toCompleteUrl(httpsProxy, false);
          }
          if (noProxy) { // Might be a comma separated list of hosts
              this._noProxyUrls = noProxy.split(',').map(item => item.trim());
          }
      }
      validateUpstreamProxy(proxyUrl, parameterName) {
          let proxyParsed = url_1.default.parse(proxyUrl);
          if (!proxyParsed.protocol || !proxyParsed.hostname || !proxyParsed.port || proxyParsed.path !== '/') {
              throw new Error('Invalid ' + parameterName + ' argument. It must be a complete URL without path. Example: http://proxy.acme.com:8080');
          }
          return true;
      }
      matchWithWildcardRule(str, rule) {
          return new RegExp('^' + rule.split('*').join('.*') + '$').test(str);
      }
      targetInNoProxy(ntlmHostUrl) {
          if (!this._noProxyUrls) {
              return false;
          }
          let match = false;
          this._noProxyUrls.forEach(rule => {
              if (this.matchWithWildcardRule(ntlmHostUrl.hostname, rule)) {
                  match = true;
              }
          });
          return match;
      }
      setUpstreamProxyConfig(ntlmHostUrl, isSSL, agentOptions) {
          let proxyUrl = null;
          if (this.targetInNoProxy(ntlmHostUrl)) {
              return false;
          }
          if (isSSL && this._httpsProxyUrl) {
              proxyUrl = this._httpsProxyUrl;
          }
          else if (this._httpProxyUrl) {
              // Use HTTP_PROXY also for HTTPS if no HTTPS_PROXY is defined
              proxyUrl = this._httpProxyUrl;
          }
          if (proxyUrl) {
              agentOptions.host = proxyUrl.hostname;
              agentOptions.port = +proxyUrl.port;
              agentOptions.secureProxy = (proxyUrl.protocol === 'https:');
              return true;
          }
          return false;
      }
      hasHttpsUpstreamProxy(ntlmHostUrl) {
          return ((this._httpProxyUrl !== undefined ||
              this._httpsProxyUrl !== undefined) &&
              this.targetInNoProxy(ntlmHostUrl) === false);
      }
      reset() {
          this._httpProxyUrl = undefined;
          this._httpsProxyUrl = undefined;
          this._noProxyUrls = undefined;
      }
  };
  UpstreamProxyManager = __decorate([
      inversify_1.injectable()
  ], UpstreamProxyManager);
  exports.UpstreamProxyManager = UpstreamProxyManager;
  