"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.LaunchDarklyUtils = 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 _LaunchDarklyUtilsMembers = require("./LaunchDarklyUtilsMembers");

var _LaunchDarklyUtilsFlags = require("./LaunchDarklyUtilsFlags");

var _LaunchDarklyUtilsRoles = require("./LaunchDarklyUtilsRoles");

var _LaunchDarklyApiClient = require("./LaunchDarklyApiClient");

var _LaunchDarklyLogger = require("./LaunchDarklyLogger");

var _dotenv = _interopRequireDefault(require("dotenv"));

var _LaunchDarklyUtilsProjects = require("./LaunchDarklyUtilsProjects");

_dotenv["default"].config();
/**
 * @class
 */


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

  (0, _createClass2["default"])(LaunchDarklyUtils, [{
    key: "create",
    value:
    /**
     * Create an instance of ldutils with api specific classes attached. This is the primary class used to access apis,
     * as api grouping util classes are attached to this class.
     * @param {string} API_TOKEN - from LaunchDarkly dashboard
     * @param { Object } customLogger - logger implementation, or 'console'. If not supplied, defaults to Bunyan logger
     * @param { string } swaggerYamlString - optional serialized yaml
     * @returns {Promise}
     * @fulfil {LaunchDarklyUtils}
     * @reject {Error} object with message
     */
    function () {
      var _create = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(API_TOKEN, customLogger, swaggerYamlString) {
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                // setup logger
                this.log = customLogger ? customLogger : _LaunchDarklyLogger.LaunchDarklyLogger.logger();
                this.log.debug('logger attached..'); // create LaunchDarkly apiClient

                _context.prev = 2;
                _context.next = 5;
                return _LaunchDarklyApiClient.LaunchDarklyApiClient.create(API_TOKEN, this.log, swaggerYamlString);

              case 5:
                this.apiClient = _context.sent;
                this.log.debug('api client instantiated..'); // attach utils

                this.flags = new _LaunchDarklyUtilsFlags.LaunchDarklyUtilsFlags(this.apiClient, this.log, this);
                this.roles = new _LaunchDarklyUtilsRoles.LaunchDarklyUtilsRoles(this.apiClient, this.log, this);
                this.members = new _LaunchDarklyUtilsMembers.LaunchDarklyUtilsMembers(this.apiClient, this.log, this);
                this.projects = new _LaunchDarklyUtilsProjects.LaunchDarklyUtilsProjects(this.apiClient, this.log, this);
                this.log.debug("utils ready.");
                _context.next = 18;
                break;

              case 14:
                _context.prev = 14;
                _context.t0 = _context["catch"](2);
                this.log.error(_context.t0);
                throw _context.t0;

              case 18:
                return _context.abrupt("return", this);

              case 19:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this, [[2, 14]]);
      }));

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

      return create;
    }()
  }]);
  return LaunchDarklyUtils;
}();

exports.LaunchDarklyUtils = LaunchDarklyUtils;