'use strict';

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

var _regenerator;

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

var fetchRemoteVersions = exports.fetchRemoteVersions = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(scope, componentIds) {
    var externals, remotes;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            externals = componentIds.filter(function (id) {
              return !id.isLocal(scope.name);
            });
            _context.next = 3;
            return getScopeRemotes(scope);

          case 3:
            remotes = _context.sent;
            return _context.abrupt('return', remotes.latestVersions(externals, scope));

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

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

exports.getScopeRemotes = getScopeRemotes;

var _ramda;

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

var _globalRemotes;

function _load_globalRemotes() {
  return _globalRemotes = _interopRequireDefault(require('../global-config/global-remotes'));
}

var _remotes;

function _load_remotes() {
  return _remotes = require('../remotes');
}

var _bitId;

function _load_bitId() {
  return _bitId = require('../bit-id');
}

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"); }); }; }

function getScopeRemotes(scope) {
  function mergeRemotes(globalRemotes) {
    var globalObj = globalRemotes.toPlainObject();
    return (_remotes || _load_remotes()).Remotes.load((_ramda || _load_ramda()).default.merge(globalObj, scope.scopeJson.remotes));
  }

  return (_globalRemotes || _load_globalRemotes()).default.load().then(mergeRemotes);
}