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

  _scope = function () {
    return data;
  };

  return data;
}

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

  _bitId = function () {
    return data;
  };

  return data;
}

function _loader() {
  const data = _interopRequireDefault(require("../../../cli/loader"));

  _loader = function () {
    return data;
  };

  return data;
}

function _loaderMessages() {
  const data = require("../../../cli/loader/loader-messages");

  _loaderMessages = function () {
    return data;
  };

  return data;
}

function _remotes() {
  const data = _interopRequireDefault(require("../../../remotes/remotes"));

  _remotes = function () {
    return data;
  };

  return data;
}

function _remote() {
  const data = _interopRequireDefault(require("../../../remotes/remote"));

  _remote = function () {
    return data;
  };

  return data;
}

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

  _component = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = require("../../../consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _scopeRemotes() {
  const data = require("../../../scope/scope-remotes");

  _scopeRemotes = function () {
    return data;
  };

  return data;
}

function _scopeComponentsImporter() {
  const data = _interopRequireDefault(require("../../../scope/component-ops/scope-components-importer"));

  _scopeComponentsImporter = function () {
    return data;
  };

  return data;
}

var _default =
/*#__PURE__*/
function () {
  var _getScopeComponent = (0, _bluebird().coroutine)(function* ({
    id,
    allVersions,
    scopePath,
    showDependents,
    showDependencies
  }) {
    const bitId = _bitId().BitId.parse(id, true); // user used --remote so we know it has a scope


    if (scopePath) {
      // coming from the api
      const scope = yield (0, _scope().loadScope)(scopePath);
      const component = yield showComponentUsingScope(scope);
      return {
        component
      };
    }

    const consumer = yield (0, _consumer().loadConsumerIfExist)(); // $FlowFixMe

    const remote = yield getRemote();

    _loader().default.start(_loaderMessages().BEFORE_REMOTE_SHOW);

    const component = yield remote.show(bitId);
    let dependenciesInfo = [];
    let dependentsInfo = [];

    if (showDependents || showDependencies) {
      const componentDepGraph = yield remote.graph(component.id);

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

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

    return {
      component,
      dependentsInfo,
      dependenciesInfo
    };

    function showComponentUsingScope(_x2) {
      return _showComponentUsingScope.apply(this, arguments);
    }

    function _showComponentUsingScope() {
      _showComponentUsingScope = (0, _bluebird().coroutine)(function* (scope) {
        if (allVersions) {
          return scope.loadAllVersions(bitId);
        }

        const scopeComponentsImporter = _scopeComponentsImporter().default.getInstance(scope);

        return scopeComponentsImporter.loadRemoteComponent(bitId);
      });
      return _showComponentUsingScope.apply(this, arguments);
    }

    function getRemote() {
      return _getRemote.apply(this, arguments);
    }

    function _getRemote() {
      _getRemote = (0, _bluebird().coroutine)(function* () {
        // $FlowFixMe scope must be set as it came from a remote
        const scopeName = bitId.scope;

        if (consumer) {
          const remotes = yield (0, _scopeRemotes().getScopeRemotes)(consumer.scope);
          return remotes.resolve(scopeName, consumer.scope);
        }

        return _remotes().default.getScopeRemote(scopeName);
      });
      return _getRemote.apply(this, arguments);
    }
  });

  function getScopeComponent(_x) {
    return _getScopeComponent.apply(this, arguments);
  }

  return getScopeComponent;
}();

exports.default = _default;