"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.LaunchDarklyApiClient = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _swaggerClient = _interopRequireDefault(require("swagger-client"));

var _jsYaml = _interopRequireDefault(require("js-yaml"));

var _fs = _interopRequireDefault(require("fs"));

var _formatJson = _interopRequireDefault(require("format-json"));

var _httpsProxyAgent = _interopRequireDefault(require("https-proxy-agent"));

var _nodeFetch = _interopRequireDefault(require("node-fetch"));

/**
 * @class
 */
var LaunchDarklyApiClient = /*#__PURE__*/function () {
  function LaunchDarklyApiClient() {
    (0, _classCallCheck2["default"])(this, LaunchDarklyApiClient);
  }

  (0, _createClass2["default"])(LaunchDarklyApiClient, null, [{
    key: "create",
    value:
    /**
     * Used internally by LaunchDarklyUtils to create an instance of
     * Swagger apiClient with interceptors configured
     * @param {string} API_TOKEN - from LaunchDarkly dashboard
     * @param { Object } log - logger implementation
     * @param { string } swaggerYamlString - optional serialized yaml
     * @returns {Promise}
     * @fulfil {Swagger} apiClient generated by swagger-js
     * @reject {Error} object with message
     */
    function () {
      var _create = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(API_TOKEN, log, swaggerYamlString) {
        var proxy, agent, swaggerYaml, swaggerJson;
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                log.debug("creating api client with token: ".concat(API_TOKEN));
                proxy = process.env.http_proxy || process.env.https_proxy || null;
                agent = null;

                if (proxy) {
                  agent = new _httpsProxyAgent["default"](proxy);
                  log.debug("using proxy from env var https_proxy=".concat(proxy));
                } // swagger.yaml from https://launchdarkly.github.io/ld-openapi/swagger.yaml


                swaggerYaml = swaggerYamlString || _fs["default"].readFileSync(__dirname + "/../swagger.yaml", 'utf-8').toString();
                swaggerJson = _jsYaml["default"].safeLoad(swaggerYaml);
                return _context.abrupt("return", (0, _swaggerClient["default"])({
                  spec: swaggerJson,
                  usePromise: true,
                  requestInterceptor: function requestInterceptor(req) {
                    req.userFetch = _nodeFetch["default"];
                    req.agent = agent;
                    req.headers.Authorization = API_TOKEN;
                    log.debug("REQUEST: ".concat(_formatJson["default"].plain(req)));
                    return req;
                  },
                  responseInterceptor: function responseInterceptor(res) {
                    log.debug("RESPONSE: ".concat(_formatJson["default"].plain(res)));

                    if (res.status > 299) {
                      log.debug("ERROR: ".concat(_formatJson["default"].plain(res)));
                      throw res;
                    }

                    return res;
                  }
                }));

              case 7:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      function create(_x, _x2, _x3) {
        return _create.apply(this, arguments);
      }

      return create;
    }()
  }]);
  return LaunchDarklyApiClient;
}();

exports.LaunchDarklyApiClient = LaunchDarklyApiClient;