import {keys} from 'lodash';
import {combineReducers} from 'redux';
import {handleActions} from 'redux-actions';
import {UnitActions, IceSkatingServices, SkiingServices, QualityEnum} from './constants';
import {getUnitQuality, enumerableQuality} from './helpers';
import {EntityAction} from '../common/constants';

const isFetchingReducer = handleActions({
  [UnitActions.FETCH]: () => true,
  [UnitActions.RECEIVE]: () => false
}, false);

const byIdReducer = handleActions({
  [UnitActions.RECEIVE]: (state: Object, {payload: {entities}}: EntityAction) =>
    ({...entities.unit})
}, {});

const all = handleActions({
  [UnitActions.RECEIVE]: (state: Object, {payload: {entities}}: EntityAction) =>
    [...keys(entities.unit)]
}, []);

const iceskate = handleActions({
  [UnitActions.RECEIVE]: (state: Object, {payload: {entities}}: EntityAction) =>
    [...keys(entities.unit).filter((id) => entities.unit[id].services.some((unitService) => IceSkatingServices.indexOf(unitService.id) !== -1))]
}, []);

const ski = handleActions({
  [UnitActions.RECEIVE]: (state: Object, {payload: {entities}}: EntityAction) =>
    [...keys(entities.unit).filter((id) => entities.unit[id].services.some((unitService) => SkiingServices.indexOf(unitService.id) !== -1))]
}, []);

const openNow = handleActions({
  [UnitActions.RECEIVE]: (state: Object, {payload: {entities}}: EntityAction) =>
    [...keys(entities.unit).filter((id) => enumerableQuality(getUnitQuality(entities.unit[id])) <= QualityEnum.satisfactory)]
}, []);

const searchResults = handleActions({
  [UnitActions.SEARCH_RECEIVE]: (state: Object, {payload: {entities}}: EntityAction) =>
    entities ? [...keys(entities.unit)] : [],
  [UnitActions.SEARCH_CLEAR]: () => []
}, []);

const searchSuggestions = handleActions({
  [UnitActions.RECEIVE_SEARCH_SUGGESTIONS]: (state: Object, {payload: {entities}}: EntityAction) =>
    entities ? [...keys(entities.unit)] : [],
  [UnitActions.SEARCH_RECEIVE]: () => [],
  [UnitActions.SEARCH_CLEAR]: () => []
}, []);

const searchActive = handleActions({
  [UnitActions.SEARCH_RECEIVE]: () => true,
  [UnitActions.SEARCH_CLEAR]: () => false
}, false);

const reducer = combineReducers({
  isFetching: isFetchingReducer,
  byId: byIdReducer,
  all,
  iceskate,
  ski,
  open_now: openNow,
  searchResults,
  searchSuggestions,
  searchActive
});

export default reducer;

@@ -1511,641 +1511,8 @@
 );%0A%0A
-const searchResults = handleActions(%7B%0A  %5BUnitActions.SEARCH_RECEIVE%5D: (state: Object, %7Bpayload: %7Bentities%7D%7D: EntityAction) =%3E%0A    entities ? %5B...keys(entities.unit)%5D : %5B%5D,%0A  %5BUnitActions.SEARCH_CLEAR%5D: () =%3E %5B%5D%0A%7D, %5B%5D);%0A%0Aconst searchSuggestions = handleActions(%7B%0A  %5BUnitActions.RECEIVE_SEARCH_SUGGESTIONS%5D: (state: Object, %7Bpayload: %7Bentities%7D%7D: EntityAction) =%3E%0A    entities ? %5B...keys(entities.unit)%5D : %5B%5D,%0A  %5BUnitActions.SEARCH_RECEIVE%5D: () =%3E %5B%5D,%0A  %5BUnitActions.SEARCH_CLEAR%5D: () =%3E %5B%5D%0A%7D, %5B%5D);%0A%0Aconst searchActive = handleActions(%7B%0A  %5BUnitActions.SEARCH_RECEIVE%5D: () =%3E true,%0A  %5BUnitActions.SEARCH_CLEAR%5D: () =%3E false%0A%7D, false);%0A%0A
 cons
@@ -1644,62 +1644,8 @@
 nNow
-,%0A  searchResults,%0A  searchSuggestions,%0A  searchActive
 %0A%7D);

Remove unused reducers