import getMockBingSearchResults from 'js/components/Search/getMockBingSearchResults'
import { getSearchResultCountPerPage } from 'js/utils/search-utils'

/**
 * Call our search API endpoint.
 * @param {String} query - The search query, unencoded.
 * @return {Object}
 */
const fetchBingSearchResults = async (query = null) => {
  if (!query) {
    throw new Error(`Search query must be a non-empty string.`)
  }
  if (
    process.env.NODE_ENV === 'development' &&
    process.env.REACT_APP_MOCK_SEARCH_RESULTS === 'true'
  ) {
    // Mock search results, including network delay.
    return new Promise(resolve => {
      setTimeout(() => resolve(getMockBingSearchResults()), 400)
    })
  }
  try {
    const endpoint = process.env.REACT_APP_SEARCH_QUERY_ENDPOINT
    if (!endpoint) {
      throw new Error('Search query endpoint is not defined.')
    }

    // Possible values:
    // Computation, Entities, Images, News, RelatedSearches, SpellSuggestions,
    // TimeZone, Videos, Webpages
    // Add to filters as we support displaying a cateogry.
    const responseFilter = 'Webpages,News'
    const searchURL = `${endpoint}?q=${encodeURI(
      query
    )}&count=${getSearchResultCountPerPage()}&responseFilter=${responseFilter}`
    return fetch(searchURL, {
      method: 'GET',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    })
      .then(response => {
        return response
          .json()
          .then(response => response)
          .catch(e => {
            throw e
          })
      })
      .catch(e => {
        throw e
      })
  } catch (e) {
    throw e
  }
}

export default fetchBingSearchResults

@@ -1,12 +1,32 @@
+import qs from 'qs'%0A
 import getMo
@@ -98,16 +98,16 @@
 esults'%0A
-
 import %7B
@@ -165,16 +165,82 @@
 h-utils'
+%0A// import %7B getBingClientID %7D from 'js/utils/local-user-data-mgr'
 %0A%0A/**%0A *
@@ -936,16 +936,288 @@
 %0A    %7D%0A%0A
+    // TODO: send the Bing client ID if one exists.%0A    // const bingClientID = getBingClientID()%0A    const params = %7B%0A      q: query,%0A      count: getSearchResultCountPerPage(),%0A    %7D%0A    const searchURL = %60$%7Bendpoint%7D?$%7Bqs.stringify(params)%7D&$%7Bqs.stringify(%0A      %7B%0A    
     // P
@@ -1232,16 +1232,20 @@
 values:%0A
+    
     // C
@@ -1315,16 +1315,20 @@
 stions,%0A
+    
     // T
@@ -1361,67 +1361,107 @@
     
-// Add to filters as we support displaying a cateog
+    // Makes sure commas for list items are not encoded.%0A        // Bing makes this mandato
 ry.%0A    
 cons
@@ -1456,21 +1456,19 @@
 ry.%0A    
-const
+   
  respons
@@ -1474,18 +1474,17 @@
 seFilter
- =
+:
  'Webpag
@@ -1495,151 +1495,74 @@
 ews'
+,
 %0A    
-const searchURL = %60$%7Bendpoint%7D?q=$%7BencodeURI(%0A      query%0A    )%7D&count=$%7BgetSearchResultCountPerPage()%7D&responseFilter=$%7BresponseFilter
+  %7D,%0A      %7B arrayFormat: 'comma', encode: false %7D%0A    )
 %7D%60%0A
+%0A
     

Use qs to stringify URL params