"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = cacheAdapterEnhancer;

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

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

var _mobx = require("mobx");

var _configure = require("../configure");

var _cache = _interopRequireWildcard(require("../cache"));

var _utils = require("./utils");

function getDefaultCache() {
  var lookupCache = (0, _mobx.toJS)((0, _configure.getConfig)('lookupCache'));
  var cache = new _cache["default"](lookupCache);
  (0, _cache.refreshCacheOptions)(cache);
  return cache;
}

function cacheAdapterEnhancer(adapter) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var _options$enabledByDef = options.enabledByDefault,
      enabledByDefault = _options$enabledByDef === void 0 ? true : _options$enabledByDef,
      _options$cacheFlag = options.cacheFlag,
      cacheFlag = _options$cacheFlag === void 0 ? 'cache' : _options$cacheFlag,
      _options$defaultCache = options.defaultCache,
      defaultCache = _options$defaultCache === void 0 ? getDefaultCache() : _options$defaultCache;
  return function (config) {
    var useCache = config[cacheFlag] !== undefined && config[cacheFlag] !== null ? config[cacheFlag] : enabledByDefault;

    if (useCache) {
      var cache = (0, _utils.isCacheLike)(useCache) ? useCache : defaultCache;
      var index = (0, _utils.buildURLWithAxiosConfig)(config);
      var responsePromise = cache.get(index);

      if (!responsePromise) {
        responsePromise = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
          return _regenerator["default"].wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.prev = 0;
                  _context.next = 3;
                  return adapter(config);

                case 3:
                  return _context.abrupt("return", _context.sent);

                case 6:
                  _context.prev = 6;
                  _context.t0 = _context["catch"](0);
                  cache.del(index);
                  throw _context.t0;

                case 10:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee, null, [[0, 6]]);
        }))();

        if (process.env.LOGGER_LEVEL === 'info') {
          // eslint-disable-next-line no-console
          console.info("request: ".concat(index));
        }

        cache.set(index, responsePromise);
        return responsePromise;
      }

      if (process.env.LOGGER_LEVEL === 'info') {
        // eslint-disable-next-line no-console
        console.info("request cached by cache adapter: ".concat(index));
      }

      return responsePromise;
    }

    return adapter(config);
  };
}
//# sourceMappingURL=cacheAdapterEnhancer.js.map
