'use strict';

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

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

var _regenerator2 = _interopRequireDefault(_regenerator);

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 _swaggerClient = require('swagger-client');

var _swaggerClient2 = _interopRequireDefault(_swaggerClient);

var _jsYaml = require('js-yaml');

var _jsYaml2 = _interopRequireDefault(_jsYaml);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _appRootPath = require('app-root-path');

var _appRootPath2 = _interopRequireDefault(_appRootPath);

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

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

    (0, _createClass3.default)(LaunchDarklyApiClient, null, [{
        key: 'create',
        value: function () {
            var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(API_TOKEN, log) {
                var yaml, json;
                return _regenerator2.default.wrap(function _callee$(_context) {
                    while (1) {
                        switch (_context.prev = _context.next) {
                            case 0:
                                log.info('creating client with api token: ' + API_TOKEN);

                                // swagger.yaml from https://launchdarkly.github.io/ld-openapi/swagger.yaml
                                yaml = _fs2.default.readFileSync(_appRootPath2.default + '/swagger.yaml', 'utf-8').toString();
                                json = _jsYaml2.default.safeLoad(yaml);
                                return _context.abrupt('return', (0, _swaggerClient2.default)({
                                    spec: json,
                                    usePromise: true,
                                    requestInterceptor: function requestInterceptor(req) {
                                        req.headers.Authorization = API_TOKEN;

                                        // hack - fix incorrect qs mapping..
                                        req.url = req.url.replace('environmentKeyQuery', 'env');

                                        return req;
                                    }
                                }));

                            case 4:
                            case 'end':
                                return _context.stop();
                        }
                    }
                }, _callee, this);
            }));

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

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