"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

  _bluebird = function () {
    return data;
  };

  return data;
}

function _ramda() {
  const data = _interopRequireDefault(require("ramda"));

  _ramda = function () {
    return data;
  };

  return data;
}

function _consumerComponent() {
  const data = _interopRequireDefault(require("../component/consumer-component"));

  _consumerComponent = function () {
    return data;
  };

  return data;
}

function _componentDependencies() {
  const data = _interopRequireDefault(require("../../scope/component-dependencies"));

  _componentDependencies = function () {
    return data;
  };

  return data;
}

function _() {
  const data = require("..");

  _ = function () {
    return data;
  };

  return data;
}

function _bitIds() {
  const data = _interopRequireDefault(require("../../bit-id/bit-ids"));

  _bitIds = function () {
    return data;
  };

  return data;
}

function _bitId() {
  const data = _interopRequireDefault(require("../../bit-id/bit-id"));

  _bitId = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../../constants");

  _constants = function () {
    return data;
  };

  return data;
}

var _default =
/*#__PURE__*/
function () {
  var _loadFlattenedDependencies = (0, _bluebird().coroutine)(function* (consumer, component, forCapsule = false) {
    const dependencies = yield loadManyDependencies(component.dependencies.getAllIds());
    const devDependencies = yield loadManyDependencies(component.devDependencies.getAllIds());
    const compilerDependencies = yield loadManyDependencies(component.compilerDependencies.getAllIds());
    const testerDependencies = yield loadManyDependencies(component.testerDependencies.getAllIds());
    yield loadFlattened(dependencies);
    yield loadFlattened(devDependencies);
    yield loadFlattened(compilerDependencies);
    yield loadFlattened(testerDependencies);
    return new (_componentDependencies().default)({
      component,
      dependencies,
      devDependencies,
      compilerDependencies,
      testerDependencies
    });

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

    function _loadManyDependencies() {
      _loadManyDependencies = (0, _bluebird().coroutine)(function* (dependenciesIds) {
        return Promise.all(dependenciesIds.map(dep => loadDependency(dep)));
      });
      return _loadManyDependencies.apply(this, arguments);
    }

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

    function _loadDependency() {
      _loadDependency = (0, _bluebird().coroutine)(function* (dependencyId) {
        const componentMap = consumer.bitMap.getComponentIfExist(dependencyId);

        const couldBeModified = componentMap && componentMap.origin !== _constants().COMPONENT_ORIGINS.NESTED;

        if (couldBeModified) {
          return forCapsule ? consumer.loadComponentForCapsule(dependencyId) : consumer.loadComponent(dependencyId);
        }

        const componentFromModel = yield consumer.loadComponentFromModel(dependencyId);
        return forCapsule ? componentFromModel.clone() : componentFromModel;
      });
      return _loadDependency.apply(this, arguments);
    }

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

    function _loadFlattened() {
      _loadFlattened = (0, _bluebird().coroutine)(function* (deps) {
        if (_ramda().default.isEmpty(deps)) return;
        yield loadFlattenedFromModel(deps);
        yield loadFlattenedFromFsRecursively(deps);
      });
      return _loadFlattened.apply(this, arguments);
    }

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

    function _loadFlattenedFromModel() {
      _loadFlattenedFromModel = (0, _bluebird().coroutine)(function* (deps) {
        const dependenciesFromModel = deps.filter(d => !d.loadedFromFileSystem);
        const flattenedIdsFromModel = dependenciesFromModel.map(d => d.flattenedDependencies);
        const flattenedFromModel = yield loadManyDependencies(_ramda().default.flatten(flattenedIdsFromModel));
        deps.push(...flattenedFromModel);
      });
      return _loadFlattenedFromModel.apply(this, arguments);
    }

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

    function _loadFlattenedFromFsRecursively() {
      _loadFlattenedFromFsRecursively = (0, _bluebird().coroutine)(function* (components) {
        const currentIds = _bitIds().default.fromArray(components.map(c => c.id));

        const ids = _ramda().default.flatten(components.filter(c => c.loadedFromFileSystem).map(c => c.dependencies.getAllIds()));

        const idsUniq = _bitIds().default.uniqFromArray(ids);

        const newIds = idsUniq.filter(id => !currentIds.has(id));
        if (_ramda().default.isEmpty(newIds)) return;
        const deps = yield loadManyDependencies(newIds);
        if (_ramda().default.isEmpty(deps)) return;
        components.push(...deps);
        yield loadFlattened(components);
      });
      return _loadFlattenedFromFsRecursively.apply(this, arguments);
    }
  });

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

  return loadFlattenedDependencies;
}();

exports.default = _default;