'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 _formatJson = require('format-json');

var _formatJson2 = _interopRequireDefault(_formatJson);

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

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

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

        /**
         * 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
         */
        value: function () {
            var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(API_TOKEN, log, swaggerYamlString) {
                var swaggerYaml, swaggerJson;
                return _regenerator2.default.wrap(function _callee$(_context) {
                    while (1) {
                        switch (_context.prev = _context.next) {
                            case 0:
                                log.debug('creating api client with token: ' + API_TOKEN);

                                // swagger.yaml from https://launchdarkly.github.io/ld-openapi/swagger.yaml
                                swaggerYaml = swaggerYamlString || _fs2.default.readFileSync(__dirname + '/../swagger.yaml', 'utf-8').toString();
                                swaggerJson = _jsYaml2.default.safeLoad(swaggerYaml);
                                return _context.abrupt('return', (0, _swaggerClient2.default)({
                                    spec: swaggerJson,
                                    usePromise: true,
                                    requestInterceptor: function requestInterceptor(req) {
                                        req.headers.Authorization = API_TOKEN;
                                        log.debug('REQUEST: ' + _formatJson2.default.plain(req));

                                        return req;
                                    },
                                    responseInterceptor: function responseInterceptor(res) {
                                        log.debug('RESPONSE: ' + _formatJson2.default.plain(res));

                                        if (res.status !== 200) {
                                            log.debug('ERROR: ' + _formatJson2.default.plain(res));
                                            throw res;
                                        }

                                        return res;
                                    }
                                }));

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

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

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