'use strict';

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

var _indexer;

function _load_indexer() {
  return _indexer = require('./indexer');
}

var _stopwords;

function _load_stopwords() {
  return _stopwords = _interopRequireDefault(require('./stopwords'));
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var boost = {
  name: 5,
  tokenizedName: 4,
  stemmedName: 3,
  functionNames: 2,
  tokenizedFunctionNames: 2,
  minDescription: 1,
  stemmedMinDescription: 0.5
};

function queryItem(field, queryStr) {
  return {
    AND: { [field]: queryStr.toLowerCase().split(' ') },
    BOOST: boost[field]
  };
}

function buildQuery(queryStr) {
  var queryStrWithoutStopwords = queryStr.split(' ').filter(function (word) {
    return !(_stopwords || _load_stopwords()).default.includes(word);
  }).join(' ');
  var tokenizedQuery = (0, (_indexer || _load_indexer()).tokenizeStr)(queryStr);
  var query = [];
  query.push(queryItem('name', queryStr));
  query.push(queryItem('tokenizedName', tokenizedQuery));
  query.push(queryItem('stemmedName', (0, (_indexer || _load_indexer()).stem)(tokenizedQuery)));
  query.push(queryItem('functionNames', queryStr));
  query.push(queryItem('tokenizedFunctionNames', tokenizedQuery));
  query.push(queryItem('minDescription', queryStrWithoutStopwords));
  query.push(queryItem('stemmedMinDescription', (0, (_indexer || _load_indexer()).stem)(queryStrWithoutStopwords)));
  return query;
}

exports.default = buildQuery;