"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 _consumer() {
  const data = require("../../../consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _nothingToCompareTo() {
  const data = _interopRequireDefault(require("./exceptions/nothing-to-compare-to"));

  _nothingToCompareTo = function () {
    return data;
  };

  return data;
}

function _scopeGraph() {
  const data = _interopRequireDefault(require("../../../scope/graph/scope-graph"));

  _scopeGraph = function () {
    return data;
  };

  return data;
}

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

  _componentsList = function () {
    return data;
  };

  return data;
}

var _default =
/*#__PURE__*/
function () {
  var _getConsumerBit = (0, _bluebird().coroutine)(function* ({
    id,
    compare,
    allVersions,
    showRemoteVersions,
    showDependents,
    showDependencies
  }) {
    const consumer = yield (0, _consumer().loadConsumer)();
    const bitId = consumer.getParsedId(id);

    if (allVersions) {
      return consumer.loadAllVersionsOfComponentFromModel(bitId);
    }

    const component = yield consumer.loadComponent(bitId); // loads recent component

    let dependenciesInfo = [];
    let dependentsInfo = [];

    if (showDependents || showDependencies) {
      const componentsList = new (_componentsList().default)(consumer);
      const allComponents = yield componentsList.getFromFileSystem();

      const graph = _scopeGraph().default.buildGraphFromComponents(allComponents);

      const dependencyGraph = new (_scopeGraph().default)(graph);
      const componentGraph = dependencyGraph.getSubGraphOfConnectedComponents(component.id);
      const componentDepGraph = new (_scopeGraph().default)(componentGraph);

      if (showDependents) {
        dependentsInfo = componentDepGraph.getDependentsInfo(component.id);
      }

      if (showDependencies) {
        dependenciesInfo = componentDepGraph.getDependenciesInfo(component.id);
      }
    }

    if (showRemoteVersions) {
      yield consumer.addRemoteAndLocalVersionsToDependencies(component, true);
    }

    if (compare) {
      if (!component.componentFromModel) throw new (_nothingToCompareTo().default)(id);
      return {
        component,
        componentModel: component.componentFromModel
      };
    }

    yield consumer.onDestroy();
    return {
      component,
      dependentsInfo,
      dependenciesInfo
    };
  });

  function getConsumerBit(_x) {
    return _getConsumerBit.apply(this, arguments);
  }

  return getConsumerBit;
}();

exports.default = _default;