'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.LaunchDarklyUtils = undefined;

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _LaunchDarklyUtilsMembers = require('./LaunchDarklyUtilsMembers');

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

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

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

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

var _dotenv = require('dotenv');

var _dotenv2 = _interopRequireDefault(_dotenv);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

_dotenv2.default.config();

/**
 * @class
 */

var LaunchDarklyUtils = exports.LaunchDarklyUtils = function () {
    function LaunchDarklyUtils() {
        (0, _classCallCheck3.default)(this, LaunchDarklyUtils);
    }

    (0, _createClass3.default)(LaunchDarklyUtils, [{
        key: 'create',

        /**
         * 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
         */
        value: function () {
            var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(API_TOKEN, customLogger, swaggerYamlString) {
                var _this = this;

                var that;
                return _regenerator2.default.wrap(function _callee2$(_context2) {
                    while (1) {
                        switch (_context2.prev = _context2.next) {
                            case 0:
                                that = this;
                                return _context2.abrupt('return', new _promise2.default(function () {
                                    var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(resolve, reject) {
                                        return _regenerator2.default.wrap(function _callee$(_context) {
                                            while (1) {
                                                switch (_context.prev = _context.next) {
                                                    case 0:
                                                        // setup logger
                                                        that.log = customLogger ? customLogger : _LaunchDarklyLogger.LaunchDarklyLogger.logger();
                                                        that.log.debug('logger attached..');

                                                        // create LaunchDarkly apiClient
                                                        _context.prev = 2;
                                                        _context.next = 5;
                                                        return _LaunchDarklyApiClient.LaunchDarklyApiClient.create(API_TOKEN, _this.log, swaggerYamlString);

                                                    case 5:
                                                        that.apiClient = _context.sent;

                                                        that.log.debug('api client instantiated..');

                                                        // attach utils
                                                        that.flags = new _LaunchDarklyUtilsFlags.LaunchDarklyUtilsFlags(_this.apiClient, _this.log, that);
                                                        that.roles = new _LaunchDarklyUtilsRoles.LaunchDarklyUtilsRoles(_this.apiClient, _this.log, that);
                                                        that.members = new _LaunchDarklyUtilsMembers.LaunchDarklyUtilsMembers(_this.apiClient, _this.log, that);
                                                        that.log.debug('utils ready.');
                                                        _context.next = 16;
                                                        break;

                                                    case 13:
                                                        _context.prev = 13;
                                                        _context.t0 = _context['catch'](2);
                                                        return _context.abrupt('return', reject(_context.t0));

                                                    case 16:
                                                        return _context.abrupt('return', resolve(that));

                                                    case 17:
                                                    case 'end':
                                                        return _context.stop();
                                                }
                                            }
                                        }, _callee, _this, [[2, 13]]);
                                    }));

                                    return function (_x4, _x5) {
                                        return _ref2.apply(this, arguments);
                                    };
                                }()));

                            case 2:
                            case 'end':
                                return _context2.stop();
                        }
                    }
                }, _callee2, this);
            }));

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

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