"use strict";

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

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

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

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

  _ramda = function () {
    return data;
  };

  return data;
}

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

  _chalk = function () {
    return data;
  };

  return data;
}

function _command() {
  const data = _interopRequireDefault(require("../../command"));

  _command = function () {
    return data;
  };

  return data;
}

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

  _consumer = function () {
    return data;
  };

  return data;
}

function _listTemplate() {
  const data = _interopRequireDefault(require("../../templates/list-template"));

  _listTemplate = function () {
    return data;
  };

  return data;
}

function _bareListTemplate() {
  const data = _interopRequireDefault(require("../../templates/bare-list-template"));

  _bareListTemplate = function () {
    return data;
  };

  return data;
}

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

  _constants = function () {
    return data;
  };

  return data;
}

function _hasWildcard() {
  const data = _interopRequireDefault(require("../../../utils/string/has-wildcard"));

  _hasWildcard = function () {
    return data;
  };

  return data;
}

class List extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'list [scope]');
    (0, _defineProperty2().default)(this, "description", `list components on a local or a remote scope.\n  https://${_constants().BASE_DOCS_DOMAIN}/docs/cli-link.html`);
    (0, _defineProperty2().default)(this, "alias", 'ls');
    (0, _defineProperty2().default)(this, "opts", [['ids', 'ids', 'show only component ids unformatted'], ['s', 'scope', 'show all components of the scope, including indirect dependencies'], ['b', 'bare', 'DEPRECATED. use --raw instead'], ['r', 'raw', 'show raw output (only components ids, no styling)'], ['o', 'outdated', 'show latest versions from remotes'], ['j', 'json', 'show the output in JSON format'], ['n', 'namespace <string>', 'show only specified namespace by using wildcards']]);
    (0, _defineProperty2().default)(this, "loader", true);
    (0, _defineProperty2().default)(this, "migration", true);
  }

  action([scopeName], {
    ids,
    scope = false,
    bare = false,
    raw = false,
    json = false,
    outdated = false,
    namespace
  }) {
    const params = {
      scopeName,
      showAll: scope,
      showRemoteVersion: outdated
    };

    if (bare) {
      console.warn(_chalk().default.yellow('--bare flag is deprecated. please use --raw instead')); // eslint-disable-line no-console

      raw = true;
    }

    if (namespace) {
      const namespaceWithWildcard = (0, _hasWildcard().default)(namespace) ? namespace : `${namespace}/*`; // $FlowFixMe

      params.namespacesUsingWildcards = namespaceWithWildcard;
    }

    return (0, _consumer().listScope)(params).then(listScopeResults => ({
      listScopeResults,
      scope: scopeName,
      ids,
      raw,
      json,
      outdated
    }));
  }

  report({
    listScopeResults,
    scope,
    ids,
    raw,
    json,
    outdated
  }) {
    function decideHeaderSentence() {
      if (json) return '';
      if (!scope) return `found ${listScopeResults.length} components in local scope\n`;
      return _chalk().default.white(`found ${listScopeResults.length} components in ${_chalk().default.bold(scope)}\n`);
    }

    if (_ramda().default.isEmpty(listScopeResults)) {
      return _chalk().default.white(json ? '[]' : `${decideHeaderSentence()}`);
    }

    if (ids) return JSON.stringify(listScopeResults.map(result => result.id.toString())); // TODO - use a cheaper list for ids flag (do not fetch versions at all) @!IMPORTANT

    return decideHeaderSentence() + (raw ? (0, _bareListTemplate().default)(listScopeResults) : (0, _listTemplate().default)(listScopeResults, json, outdated));
  }

}

exports.default = List;