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

  _exceptions = 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
  }) {
    const bitId = _bitId().BitId.parse(id, true); // user used --remote so we know it has a scope


    const remoteShow =
    /*#__PURE__*/
    function () {
      var _ref = (0, _bluebird().coroutine)(function* (remote) {
        _loader().default.start(_loaderMessages().BEFORE_REMOTE_SHOW);

        return remote.show(bitId);
      });

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

    const getConsumer =
    /*#__PURE__*/
    function () {
      var _ref2 = (0, _bluebird().coroutine)(function* () {
        try {
          const consumer = yield (0, _consumer().loadConsumer)();
          return consumer;
        } catch (err) {
          return null;
        }
      });

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

    const getScope =
    /*#__PURE__*/
    function () {
      var _ref3 = (0, _bluebird().coroutine)(function* () {
        try {
          const scope = yield (0, _scope().loadScope)(scopePath || process.cwd());
          return scope;
        } catch (err) {
          if (err instanceof _exceptions().ScopeNotFound) return null;
          throw err;
        }
      });

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

    const showComponentUsingConsumer =
    /*#__PURE__*/
    function () {
      var _ref4 = (0, _bluebird().coroutine)(function* (consumer) {
        if (allVersions) {
          return Promise.reject(new Error('cant list all versions of a remote scope'));
        }

        const remotes = yield (0, _scopeRemotes().getScopeRemotes)(consumer.scope); // $FlowFixMe scope must be set as it came from a remote

        const remote = yield remotes.resolve(bitId.scope, consumer.scope);
        return remoteShow(remote);
      });

      return function showComponentUsingConsumer(_x3) {
        return _ref4.apply(this, arguments);
      };
    }();

    const showComponentUsingScope =
    /*#__PURE__*/
    function () {
      var _ref5 = (0, _bluebird().coroutine)(function* (scope) {
        if (allVersions) {
          return scope.loadAllVersions(bitId);
        }

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

        return scopeComponentsImporter.loadRemoteComponent(bitId);
      });

      return function showComponentUsingScope(_x4) {
        return _ref5.apply(this, arguments);
      };
    }();

    if (!scopePath) {
      const consumer = yield getConsumer();

      if (consumer) {
        return showComponentUsingConsumer(consumer);
      }
    }

    const scope = yield getScope();

    if (scope) {
      return showComponentUsingScope(scope);
    } // $FlowFixMe the scope must be there


    const remote = yield _remotes().default.getScopeRemote(bitId.scope);
    return remoteShow(remote);
  });

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

  return getScopeComponent;
}();

exports.default = _default;