text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  var HttpRequest = require("request");
  var url_1 = require("url");
  var common_1 = require("./common");
  var BaseClient = /** @class */ (function () {
      function BaseClient(options) {
          this.host = options.host;
          this.port = options.port;
          this.serviceName = options.serviceName;
          this.serviceVersion = options.serviceVersion;
          this.instanceId = options.instanceId;
          this.sdkInfo = options.sdkInfo;
      }
      /**
       * Make a HTTPS request to a service running on Elements.
       * It will construct a valid elements URL from its serviceName, serviceVersion,
       * and instanceId that were passed to the Instance at construction time.
       */
      BaseClient.prototype.request = function (options) {
          var headers = this.sdkInfo.headers;
          if (options.headers) {
              for (var key in options.headers) {
                  headers[key] = options.headers[key];
              }
          }
          if (options.jwt) {
              headers['Authorization'] = "Bearer " + options.jwt;
          }
          var path = this.sanitizePath("services/" + this.serviceName + "/" + this.serviceVersion + "/" + this.instanceId + "/" + options.path);
          var host = url_1.format({
              protocol: 'https',
              hostname: this.host,
              port: this.port,
              pathname: path
          });
          return new Promise(function (resolve, reject) {
              HttpRequest(host, {
                  body: JSON.stringify(options.body),
                  headers: headers,
                  method: options.method,
                  qs: options.qs,
                  useQuerystring: options.useQuerystring || false,
                  forever: true
              }, function (error, response, body) {
                  if (error) {
                      reject(error);
                  }
                  else {
                      var statusCode = response.statusCode;
                      if (statusCode >= 200 && statusCode <= 299) {
                          response.body = body;
                          resolve(response);
                      }
                      else if (statusCode >= 300 && statusCode <= 399) {
                          reject(new Error("Unsupported Redirect Response: " + statusCode));
                      }
                      else if (statusCode >= 400 && statusCode <= 599) {
                          var errJson = JSON.parse(body);
                          var statusCode_1 = response.statusCode, headers_1 = response.headers;
                          var error_1 = errJson.error, error_description = errJson.error_description, error_uri = errJson.error_uri;
                          reject(new common_1.ErrorResponse({
                              error: error_1,
                              error_description: error_description,
                              error_uri: error_uri,
                              headers: headers_1,
                              status: statusCode_1,
                          }));
                      }
                      else {
                          reject(new Error("Unsupported Response Code: " + statusCode));
                      }
                  }
              });
          });
      };
      //Cleans up the path provided
      BaseClient.prototype.sanitizePath = function (path) {
          return path
              .replace(/\/ /g, "/") //Replace space after a slash
              .replace(/ $/, "") //Remove the trailing space
              .replace(/[\/]{2,}/g, "/") //Multiple slashes are now single slashes
              .replace(/\/$/, ""); //Remove trailing slash
      };
      return BaseClient;
  }());
  exports.default = BaseClient;
  //# sourceMappingURL=base_client.js.map