<commit_before>const queryString = require('qs')
const { assign } = require('lodash')

const { authorisedRequest } = require('../../lib/authorised-request')
const config = require('../../../config')

const buildOptions = (
  isAggregation,
  searchUrl,
  body,
  entity,
) => {
  if (isAggregation) {
    return {
      url: searchUrl,
      method: 'GET',
      qs: {
        ...body,
        entity,
      },
    }
  }

  return {
    body,
    url: `${searchUrl}/${entity}`,
    method: 'POST',
  }
}

function search ({
  token,
  searchTerm: term = '',
  searchEntity,
  requestBody,
  isAggregation = true,
  limit = 10,
  page = 1,
}) {
  const searchUrl = `${config.apiRoot}/v3/search`

  const body = {
    ...requestBody,
    limit,
    term,
    offset: (page * limit) - limit,
  }

  const options = buildOptions(
    isAggregation,
    searchUrl,
    body,
    searchEntity,
  )

  return authorisedRequest(token, options)
    .then(result => {
      result.page = page
      return result
    })
}

function searchInvestments ({ token, searchTerm, page = 1, limit = 10, filters = {} }) {
  const queryParams = {
    offset: (page * limit) - limit,
    limit,
  }
  const body = assign({}, filters, {
    original_query: searchTerm,
    searchEntity: 'investment_project',
  })

  const options = {
    url: `${config.apiRoot}/v3/search/investment_project?${queryString.stringify(queryParams)}`,
    method: 'POST',
    body,
  }

  return authorisedRequest(token, options)
    .then((result) => {
      result.page = page
      return result
    })
}

function searchForeignCompanies (options) {
  const optionsUkBasedFalse = assign({}, options, { isUkBased: false })

  return searchCompanies(optionsUkBasedFalse)
}

function searchLimitedCompanies ({ token, searchTerm, page = 1, limit = 10 }) {
  const queryParams = {
    offset: (page * limit) - limit,
    limit,
  }
  const body = {
    original_query: searchTerm,
  }

  const options = {
    url: `${config.apiRoot}/v3/search/companieshousecompany?${queryString.stringify(queryParams)}`,
    method: 'POST',
    body,
  }

  return authorisedRequest(token, options)
    .then((result) => {
      result.page = page
      return result
    })
}

module.exports = {
  search,
  searchCompanies,<commit_msg>Refactor remaining search functions<commit_after>const queryString = require('qs')

const { authorisedRequest } = require('../../lib/authorised-request')
const config = require('../../../config')

const buildOptions = (
  isAggregation,
  searchUrl,
  body,
  entity,
) => {
  if (isAggregation) {
    return {
      url: searchUrl,
      method: 'GET',
      qs: {
        ...body,
        entity,
      },
    }
  }

  return {
    body,
    url: `${searchUrl}/${entity}`,
    method: 'POST',
  }
}

function search ({
  token,
  searchTerm: term = '',
  searchEntity,
  requestBody,
  isAggregation = true,
  limit = 10,
  page = 1,
}) {
  const searchUrl = `${config.apiRoot}/v3/search`

  const body = {
    ...requestBody,
    limit,
    term,
    offset: (page * limit) - limit,
  }

  const options = buildOptions(
    isAggregation,
    searchUrl,
    body,
    searchEntity,
  )

  return authorisedRequest(token, options)
    .then(result => {
      result.page = page
      return result
    })
}

function searchEntity (token, body, route, { page = 1, limit = 10 }) {
  const queryParams = {
    offset: (page * limit) - limit,
    limit,
  }

  const options = {
    body,
    url: `${config.apiRoot}/v3/search/${route}?${queryString.stringify(queryParams)}`,
    method: 'POST',
  }

  return authorisedRequest(token, options)
    .then(result => {
      result.page = page
      return result
    })
}

function searchCompanies ({ token, searchTerm, isUkBased, page = 1, limit = 10, requestBody = {} }) {
  return searchEntity(token, {
    ...requestBody,
    original_query: searchTerm,
    uk_based: isUkBased,
    isAggregation: false,
  }, 'company', { page, limit })
}

function searchInvestments ({ token, searchTerm, page = 1, limit = 10, filters = {} }) {
  return searchEntity(token, {
    ...filters,
    original_query: searchTerm,
    searchEntity: 'investment_project',
  }, 'investment_project', { page, limit })
}

function searchForeignCompanies ({ token, searchTerm, page = 1, limit = 10 }) {
  return searchCompanies({
    token,
    searchTerm,
    page,
    limit,
    isUkBased: false,
  })
}

function searchLimitedCompanies ({ token, searchTerm, page = 1, limit = 10 }) {
  return searchEntity(token, {
    original_query: searchTerm,
  }, 'companieshousecompany', { page, limit })
}

module.exports = {
  search,
  searchCompanies,
{'fraction_non_alphanumeric': 0.14264199655765922, 'fraction_numerical': 0.0068846815834767644, 'mean_word_length': 2.823190789473684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3598453', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1482, 'n_words': 340}