"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 _chalk() {
  const data = _interopRequireDefault(require("chalk"));

  _chalk = function () {
    return data;
  };

  return data;
}

function _requestify() {
  const data = _interopRequireDefault(require("requestify"));

  _requestify = function () {
    return data;
  };

  return data;
}

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

  _command = function () {
    return data;
  };

  return data;
}

function _search() {
  const data = require("../../../search");

  _search = function () {
    return data;
  };

  return data;
}

function _searcher() {
  const data = require("../../../search/searcher");

  _searcher = function () {
    return data;
  };

  return data;
}

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

  _loader = function () {
    return data;
  };

  return data;
}

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

  _constants = function () {
    return data;
  };

  return data;
}

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

  _loaderMessages = function () {
    return data;
  };

  return data;
}

class Search extends _command().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", 'search <query...>');
    (0, _defineProperty2().default)(this, "description", 'search for components by desired functionality.');
    (0, _defineProperty2().default)(this, "alias", '');
    (0, _defineProperty2().default)(this, "opts", [['s', 'scope <scopename>', 'search in scope'], ['r', 'reindex', 're-index all components']]);
    (0, _defineProperty2().default)(this, "loader", true);
    (0, _defineProperty2().default)(this, "migration", true);
  }

  action([query], {
    scope,
    reindex
  }) {
    const queryStr = query.join(' ');

    if (scope) {
      _loader().default.start((0, _loaderMessages().BEFORE_REMOTE_SEARCH)({
        scope,
        queryStr
      }));

      return _search().searchAdapter.searchRemotely(queryStr, scope, reindex).catch(() => {
        // web search
        const url = `https://${_constants().SEARCH_DOMAIN}/search/?q=${queryStr}`;
        return _requestify().default.get(url).then(response => {
          const body = response.getBody();
          return Promise.resolve(body.payload.hits);
        });
      });
    }

    return Promise.reject(_chalk().default.red('Local search is disabled for now')); // return searchAdapter.searchLocally(queryStr, reindex);
  }

  report(searchResults) {
    if (!searchResults.length) {
      return _chalk().default.yellow('no results found');
    }

    return _chalk().default.green(searchResults.map(_searcher().formatter).join('\n'));
  }

}

exports.default = Search;