import _ from 'underscore';
import { getCategoryDisplayName } from '../lib/search_helpers';

const DEFAULT_RESULTS_PER_PAGE = 10;
const LARGER_RESULTS_PER_PAGE = 220;
const DEFAULT_STATE = {
  userInput: '',
  autocompleteResults: [],
  results: [],
  activeCategory: null,
  activeCategoryName: null,
  aggregations: [],
  total: 0,
  currentPage: 0,
  totalPages: 0,
  resultsPerPage: DEFAULT_RESULTS_PER_PAGE,
  query: '',
  autoCompleteQuery: '',
  isPending: false,
  isPaginatePending: false, // if the only change is the page, note special state for rendering total
  apiError: null
};

const searchResultsReducer = function (_state, action) {
  let state = _.clone(_state);
  if (typeof state === 'undefined') {
    return DEFAULT_STATE;
  }
  // let the URL change the query and other params
  if (action.type === '@@router/UPDATE_LOCATION' && action.payload.pathname === '/search') {
    let params = action.payload.query;
    // set userInput and query from q
    let newQuery = (typeof params.q === 'string') ? params.q : '';
    state.query = newQuery;
    state.userInput = newQuery;
    // set currentPage from page
    let newPage = (typeof params.page === 'string' || typeof params.page === 'number') ? parseInt(params.page) : 0;
    // set paginate pending if page is changing
    if (newPage !== state.currentPage) state.isPaginatePending = true;
    state.currentPage = newPage;
    // set active aggs
    let activeCat = (typeof params.category === 'string') ? params.category : null;
    // if changing cat, set isAggPending to true before setting active cat
    if (state.activeCategory !== activeCat) state.isAggPending = true;
    state.activeCategory = activeCat;
    state.activeCategoryName = getCategoryDisplayName(activeCat);
    // if wrapResults and on genes, allow large request size
    state.resultsPerPage = (state.activeCategory === 'locus' && params.wrapResults === 'true') ?
      LARGER_RESULTS_PER_PAGE : DEFAULT_RESULTS_PER_PAGE;
    return state;
  }
  if (action.type === 'START_SEARCH_FETCH') {
    state.isPending = true;
    return state;
  }
  if (action.type === 'SEARCH_RESPONSE') {
    state.total = action.response.total;
    state.results = action.response.results;
    state.totalPages = Math.floor(state.total / state.resultsPerPage) + ((state.total % state.resultsPerPage === 0) ? 0 : 1);
    state.aggregations = action.response.aggregations;
    state.isPending = false;
    state.isAggPending = false;
    state.isPaginatePending = false;
    return state;
  }

  if (action.type === 'SET_USER_INPUT') {
    state.userInput = action.value;
    return state;
  }
  if (action.type === 'AUTOCOMPLETE_RESPONSE') {
    state.autocompleteResults = action.value;
    return state;
  }
  if (action.type === 'SEARCH_API_ERROR') {
    state.apiError = action.value;
    return state
  }
  return state;
};

export default searchResultsReducer;

@@ -1981,34 +1981,38 @@
 rn state;%0A  %7D%0A  
-if
+switch
  (action.type ==
@@ -2008,20 +2008,28 @@
 ion.type
- ===
+) %7B%0A    case
  'START_
@@ -2041,20 +2041,20 @@
 H_FETCH'
-) %7B%0A
+:%0A  
     stat
@@ -2069,32 +2069,34 @@
 ing = true;%0A    
+  
 return state;%0A  
@@ -2095,39 +2095,35 @@
 tate;%0A  
+ 
-%7D%0A  if (action.type ===
+   break;%0A    case
  'SEARCH
@@ -2124,36 +2124,36 @@
 SEARCH_RESPONSE'
-) %7B%0A
+:%0A  
     state.total 
@@ -2177,24 +2177,26 @@
 .total;%0A    
+ 
+ 
 state.result
@@ -2224,16 +2224,18 @@
 esults;%0A
+  
     stat
@@ -2352,24 +2352,26 @@
 0 : 1);%0A    
+  
 state.aggreg
@@ -2409,16 +2409,18 @@
 ations;%0A
+  
     stat
@@ -2436,32 +2436,34 @@
 ng = false;%0A    
+  
 state.isAggPendi
@@ -2470,24 +2470,26 @@
 ng = false;%0A
+  
     state.is
@@ -2513,24 +2513,26 @@
  false;%0A    
+  
 return state
@@ -2539,32 +2539,27 @@
 ;%0A  
+ 
-%7D%0A%0A  if (action.type ===
+   break;%0A    case
  'SE
@@ -2571,20 +2571,20 @@
 R_INPUT'
-) %7B%0A
+:%0A  
     stat
@@ -2607,32 +2607,34 @@
 tion.value;%0A    
+  
 return state;%0A  
@@ -2633,39 +2633,35 @@
 tate;%0A  
-%7D%0A  if (action.type ===
+    break;%0A    case
  'AUTOCO
@@ -2676,20 +2676,20 @@
 ESPONSE'
-) %7B%0A
+:%0A  
     stat
@@ -2722,32 +2722,34 @@
 tion.value;%0A    
+  
 return state;%0A  
@@ -2752,31 +2752,27 @@
 ;%0A  
-%7D%0A  if (action.type ===
+    break;%0A    case
  'SE
@@ -2786,20 +2786,20 @@
 I_ERROR'
-) %7B%0A
+:%0A  
     stat
@@ -2825,24 +2825,26 @@
 .value;%0A    
+  
 return state
@@ -2843,21 +2843,48 @@
 rn state
+;
 %0A  
-%7D%0A
+    break;%0A    default:%0A    
   return
@@ -2891,18 +2891,21 @@
  state;%0A
+  %7D%0A
 %7D
-;
 %0A%0Aexport

switch statement in reducer