'use strict';

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

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

var _path;

function _load_path() {
  return _path = _interopRequireDefault(require('path'));
}

var _consumer;

function _load_consumer() {
  return _consumer = _interopRequireDefault(require('./consumer'));
}

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

exports.default = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee() {
    var currentPath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : process.cwd();
    var newInstance = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
    var consumer;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!(newInstance || !loadConsumer.cache || !loadConsumer.cache[currentPath])) {
              _context.next = 6;
              break;
            }

            _context.next = 3;
            return (_consumer || _load_consumer()).default.load((_path || _load_path()).default.resolve(currentPath));

          case 3:
            consumer = _context.sent;

            if (!loadConsumer.cache) loadConsumer.cache = {};
            loadConsumer.cache[currentPath] = consumer;

          case 6:
            return _context.abrupt('return', loadConsumer.cache[currentPath]);

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

  function loadConsumer() {
    return _ref.apply(this, arguments);
  }

  return loadConsumer;
}();