'use strict';

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

var _regenerator;

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

var _path;

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

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _fsExtra;

function _load_fsExtra() {
  return _fsExtra = _interopRequireDefault(require('fs-extra'));
}

var _extension;

function _load_extension() {
  return _extension = _interopRequireDefault(require('./extension'));
}

var _consumer2;

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

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../logger/logger'));
}

var _constants;

function _load_constants() {
  return _constants = require('../constants');
}

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

/**
 * Load all extensions
 * Regular, core, globals
 */
exports.default = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2() {
    var _this = this;

    var getConsumer, consumer, consumerPath, scopePath, rawExtensions, globalBitJson, globalRawExtensions, extensions;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.prev = 0;

            (_logger || _load_logger()).default.debug('start loading extensions');

            getConsumer = function () {
              var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee() {
                var _consumer;

                return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        _context.prev = 0;
                        _context.next = 3;
                        return (0, (_consumer2 || _load_consumer()).loadConsumer)();

                      case 3:
                        _consumer = _context.sent;
                        return _context.abrupt('return', _consumer);

                      case 7:
                        _context.prev = 7;
                        _context.t0 = _context['catch'](0);
                        return _context.abrupt('return', null);

                      case 10:
                      case 'end':
                        return _context.stop();
                    }
                  }
                }, _callee, _this, [[0, 7]]);
              }));

              return function getConsumer() {
                return _ref2.apply(this, arguments);
              };
            }();

            _context2.next = 5;
            return getConsumer();

          case 5:
            consumer = _context2.sent;
            consumerPath = null;
            scopePath = null;
            rawExtensions = {};

            if (consumer) {
              rawExtensions = consumer.bitJson.extensions || {};
              consumerPath = consumer.getPath();
              scopePath = consumer.scope.path;
            }

            // Load global extensions
            _context2.next = 12;
            return _getGlobalBitJson(false);

          case 12:
            globalBitJson = _context2.sent;
            globalRawExtensions = globalBitJson && globalBitJson.extensions;
            // Merge the global with the local extensions only if exists
            // The local extension is higher priority than the global ones since they are closer to the user
            // This mechanism is for internal use by bitsrc server and should not be used by the users

            if (globalRawExtensions) {
              rawExtensions = (_ramda || _load_ramda()).default.mergeDeepLeft(rawExtensions, globalRawExtensions);
            }
            extensions = (_ramda || _load_ramda()).default.values((_ramda || _load_ramda()).default.mapObjIndexed(_loadExtension(consumerPath, scopePath), rawExtensions));
            return _context2.abrupt('return', Promise.all(extensions));

          case 19:
            _context2.prev = 19;
            _context2.t0 = _context2['catch'](0);

            (_logger || _load_logger()).default.error('loading extensions failed');
            (_logger || _load_logger()).default.error(_context2.t0);
            return _context2.abrupt('return', []);

          case 24:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, this, [[0, 19]]);
  }));

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

  return loadExtensions;
}();

/**
 * Load specific extension
 * @param {string} consumerPath
 * @param {string} scopePath
 */


var _loadExtension = function _loadExtension(consumerPath, scopePath) {
  return function () {
    var rawConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    var name = arguments[1];

    var loadArgs = {
      name,
      rawConfig: rawConfig.config,
      options: rawConfig.options,
      consumerPath,
      scopePath
    };
    return (_extension || _load_extension()).default.load(loadArgs);
  };
};

/**
 * Load the global bit.json file (in order to get the global extensions)
 * @param {boolean} throws - whether to throw an error if the file corrupted
 */
var _getGlobalBitJson = function () {
  var _ref3 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(throws) {
    var globalBitJsonPath, exists;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            globalBitJsonPath = (_path || _load_path()).default.join((_constants || _load_constants()).GLOBAL_CONFIG, (_constants || _load_constants()).BIT_JSON);
            _context3.next = 3;
            return (_fsExtra || _load_fsExtra()).default.pathExists(globalBitJsonPath);

          case 3:
            exists = _context3.sent;

            if (exists) {
              _context3.next = 6;
              break;
            }

            return _context3.abrupt('return', null);

          case 6:
            return _context3.abrupt('return', (_fsExtra || _load_fsExtra()).default.readJson(globalBitJsonPath, { throws })
            // Implementing the catch my self since the throws: false not really workging
            .catch(function (e) {
              if (throws) {
                throw e;
              }
              (_logger || _load_logger()).default.debug('error during loading global bit.json');
              (_logger || _load_logger()).default.debug(e);
              return null;
            }));

          case 7:
          case 'end':
            return _context3.stop();
        }
      }
    }, _callee3, undefined);
  }));

  return function _getGlobalBitJson(_x2) {
    return _ref3.apply(this, arguments);
  };
}();