'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 _ramda;

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

var _consumerComponent;

function _load_consumerComponent() {
  return _consumerComponent = _interopRequireDefault(require('../component/consumer-component'));
}

var _componentDependencies;

function _load_componentDependencies() {
  return _componentDependencies = _interopRequireDefault(require('../../scope/component-dependencies'));
}

var _;

function _load_() {
  return _ = require('..');
}

var _bitIds;

function _load_bitIds() {
  return _bitIds = _interopRequireDefault(require('../../bit-id/bit-ids'));
}

var _bitId;

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

var _constants;

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

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

exports.default = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee6(consumer, component) {
    var loadManyDependencies = function () {
      var _ref2 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(dependenciesIds) {
        return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                return _context.abrupt('return', Promise.all(dependenciesIds.map(function (dep) {
                  return loadDependency(dep);
                })));

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

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

    var loadDependency = function () {
      var _ref3 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee2(dependencyId) {
        var componentMap, couldBeModified;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                componentMap = consumer.bitMap.getComponentIfExist(dependencyId);
                couldBeModified = componentMap && componentMap.origin !== (_constants || _load_constants()).COMPONENT_ORIGINS.NESTED;

                if (!couldBeModified) {
                  _context2.next = 4;
                  break;
                }

                return _context2.abrupt('return', consumer.loadComponent(dependencyId));

              case 4:
                return _context2.abrupt('return', consumer.loadComponentFromModel(dependencyId));

              case 5:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

      return function loadDependency(_x4) {
        return _ref3.apply(this, arguments);
      };
    }();

    var loadFlattened = function () {
      var _ref4 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee3(deps) {
        return (_regenerator || _load_regenerator()).default.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                if (!(_ramda || _load_ramda()).default.isEmpty(deps)) {
                  _context3.next = 2;
                  break;
                }

                return _context3.abrupt('return');

              case 2:
                _context3.next = 4;
                return loadFlattenedFromModel(deps);

              case 4:
                _context3.next = 6;
                return loadFlattenedFromFsRecursively(deps);

              case 6:
              case 'end':
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }));

      return function loadFlattened(_x5) {
        return _ref4.apply(this, arguments);
      };
    }();

    var loadFlattenedFromModel = function () {
      var _ref5 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee4(deps) {
        var dependenciesFromModel, flattenedIdsFromModel, flattenedFromModel;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                dependenciesFromModel = deps.filter(function (d) {
                  return !d.loadedFromFileSystem;
                });
                flattenedIdsFromModel = dependenciesFromModel.map(function (d) {
                  return d.flattenedDependencies;
                });
                _context4.next = 4;
                return loadManyDependencies((_ramda || _load_ramda()).default.flatten(flattenedIdsFromModel));

              case 4:
                flattenedFromModel = _context4.sent;

                deps.push.apply(deps, _toConsumableArray(flattenedFromModel));

              case 6:
              case 'end':
                return _context4.stop();
            }
          }
        }, _callee4, this);
      }));

      return function loadFlattenedFromModel(_x6) {
        return _ref5.apply(this, arguments);
      };
    }();

    var loadFlattenedFromFsRecursively = function () {
      var _ref6 = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee5(components) {
        var currentIds, ids, idsUniq, newIds, deps;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                currentIds = (_bitIds || _load_bitIds()).default.fromArray(components.map(function (c) {
                  return c.id;
                }));
                ids = (_ramda || _load_ramda()).default.flatten(components.filter(function (c) {
                  return c.loadedFromFileSystem;
                }).map(function (c) {
                  return c.dependencies.getAllIds();
                }));
                idsUniq = (_bitIds || _load_bitIds()).default.uniqFromArray(ids);
                newIds = idsUniq.filter(function (id) {
                  return !currentIds.has(id);
                });

                if (!(_ramda || _load_ramda()).default.isEmpty(newIds)) {
                  _context5.next = 6;
                  break;
                }

                return _context5.abrupt('return');

              case 6:
                _context5.next = 8;
                return loadManyDependencies(newIds);

              case 8:
                deps = _context5.sent;

                if (!(_ramda || _load_ramda()).default.isEmpty(deps)) {
                  _context5.next = 11;
                  break;
                }

                return _context5.abrupt('return');

              case 11:
                components.push.apply(components, _toConsumableArray(deps));
                _context5.next = 14;
                return loadFlattened(components);

              case 14:
              case 'end':
                return _context5.stop();
            }
          }
        }, _callee5, this);
      }));

      return function loadFlattenedFromFsRecursively(_x7) {
        return _ref6.apply(this, arguments);
      };
    }();

    var dependencies, devDependencies, compilerDependencies, testerDependencies;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            _context6.next = 2;
            return loadManyDependencies(component.dependencies.getAllIds());

          case 2:
            dependencies = _context6.sent;
            _context6.next = 5;
            return loadManyDependencies(component.devDependencies.getAllIds());

          case 5:
            devDependencies = _context6.sent;
            _context6.next = 8;
            return loadManyDependencies(component.compilerDependencies.getAllIds());

          case 8:
            compilerDependencies = _context6.sent;
            _context6.next = 11;
            return loadManyDependencies(component.testerDependencies.getAllIds());

          case 11:
            testerDependencies = _context6.sent;
            _context6.next = 14;
            return loadFlattened(dependencies);

          case 14:
            _context6.next = 16;
            return loadFlattened(devDependencies);

          case 16:
            _context6.next = 18;
            return loadFlattened(compilerDependencies);

          case 18:
            _context6.next = 20;
            return loadFlattened(testerDependencies);

          case 20:
            return _context6.abrupt('return', new (_componentDependencies || _load_componentDependencies()).default({
              component,
              dependencies,
              devDependencies,
              compilerDependencies,
              testerDependencies
            }));

          case 21:
          case 'end':
            return _context6.stop();
        }
      }
    }, _callee6, this);
  }));

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

  return loadFlattenedDependencies;
}();