"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.listScope = listScope;
exports.getRemoteBitIdsByWildcards = getRemoteBitIdsByWildcards;

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

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

  _remote = function () {
    return data;
  };

  return data;
}

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

  _componentsList = function () {
    return data;
  };

  return data;
}

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

  _scopeRemotes = function () {
    return data;
  };

  return data;
}

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

  _remotes = function () {
    return data;
  };

  return data;
}

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

  _exceptions = function () {
    return data;
  };

  return data;
}

function _generalError() {
  const data = _interopRequireDefault(require("../../../error/general-error"));

  _generalError = function () {
    return data;
  };

  return data;
}

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

  _bitId = function () {
    return data;
  };

  return data;
}

function _noIdMatchWildcard() {
  const data = _interopRequireDefault(require("./exceptions/no-id-match-wildcard"));

  _noIdMatchWildcard = function () {
    return data;
  };

  return data;
}

function listScope(_x) {
  return _listScope.apply(this, arguments);
}

function _listScope() {
  _listScope = (0, _bluebird().coroutine)(function* ({
    scopeName,
    showAll,
    // include nested
    showRemoteVersion,
    namespacesUsingWildcards,
    strategiesNames
  }) {
    const consumer = yield (0, _consumer().loadConsumerIfExist)();

    if (scopeName) {
      return remoteList();
    }

    return scopeList();

    function remoteList() {
      return _remoteList.apply(this, arguments);
    }

    function _remoteList() {
      _remoteList = (0, _bluebird().coroutine)(function* () {
        const remote = yield _getRemote();

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

        return remote.list(namespacesUsingWildcards, strategiesNames);
      });
      return _remoteList.apply(this, arguments);
    }

    function scopeList() {
      return _scopeList.apply(this, arguments);
    }

    function _scopeList() {
      _scopeList = (0, _bluebird().coroutine)(function* () {
        if (!consumer) {
          throw new (_exceptions().ConsumerNotFound)();
        }

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

        const componentsList = new (_componentsList().default)(consumer);
        return componentsList.listScope(showRemoteVersion, showAll, namespacesUsingWildcards);
      });
      return _scopeList.apply(this, arguments);
    }

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

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

        return _remotes().Remotes.getScopeRemote(scopeName);
      });
      return _getRemote2.apply(this, arguments);
    }
  });
  return _listScope.apply(this, arguments);
}

function getRemoteBitIdsByWildcards(_x2) {
  return _getRemoteBitIdsByWildcards.apply(this, arguments);
}

function _getRemoteBitIdsByWildcards() {
  _getRemoteBitIdsByWildcards = (0, _bluebird().coroutine)(function* (idStr) {
    if (!idStr.includes('/')) {
      throw new (_generalError().default)(`import with wildcards expects full scope-name before the wildcards, instead, got "${idStr}"`);
    }

    const idSplit = idStr.split('/');
    const scopeName = idSplit[0];

    const namespacesUsingWildcards = _ramda().default.tail(idSplit).join('/');

    const listResult = yield listScope({
      scopeName,
      namespacesUsingWildcards
    });

    if (!listResult.length) {
      throw new (_noIdMatchWildcard().default)([idStr]);
    }

    return listResult.map(result => result.id);
  });
  return _getRemoteBitIdsByWildcards.apply(this, arguments);
}