"use strict";

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

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

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = _interopRequireDefault(require("./consumer"));

  _consumer = function () {
    return data;
  };

  return data;
}

function _exceptions() {
  const data = require("./exceptions");

  _exceptions = function () {
    return data;
  };

  return data;
}

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

function _loadConsumer() {
  _loadConsumer = (0, _bluebird().coroutine)(function* (currentPath = process.cwd(), newInstance = false) {
    // $FlowFixMe
    if (newInstance || !loadConsumer.cache || !loadConsumer.cache[currentPath]) {
      const consumer = yield _consumer().default.load(_path().default.resolve(currentPath)); // $FlowFixMe

      if (!loadConsumer.cache) loadConsumer.cache = {}; // $FlowFixMe

      loadConsumer.cache[currentPath] = consumer;
    }

    return loadConsumer.cache[currentPath];
  });
  return _loadConsumer.apply(this, arguments);
}

function loadConsumerIfExist() {
  return _loadConsumerIfExist.apply(this, arguments);
}

function _loadConsumerIfExist() {
  _loadConsumerIfExist = (0, _bluebird().coroutine)(function* (currentPath = process.cwd(), newInstance = false) {
    try {
      return yield loadConsumer(currentPath, newInstance);
    } catch (err) {
      if (err instanceof _exceptions().ConsumerNotFound) {
        return null;
      }

      throw err;
    }
  });
  return _loadConsumerIfExist.apply(this, arguments);
}