import { FILTER_GRID_DATA_BY_ROUTE, SET_ROUTE, PRE_POPULATE_STORAGE } from './reducersTypes'
import Database from '../controllers/Database'
import { ___fetchFilter, ___fetchLimit } from '../helpers'

const reducer = (state, action) => {
  switch (action.type) {
    case SET_ROUTE:
      return action.payload
    case PRE_POPULATE_STORAGE:
      Database.purgeStorage()
      if( action.route=== 'summary'  ){
        return Database.getAnalytics(action.payload)
      }
      return Database.getNotesSanitized(___fetchFilter(action.route), ___fetchLimit(action.route), action.payload)
    case FILTER_GRID_DATA_BY_ROUTE:

      if( action.payload === 'summary'  ){
        return Database.getAnalytics()
      }

      return Database.getNotesSanitized(___fetchFilter(action.payload), ___fetchLimit(action.payload))
    default:
      return state
  }
}

export default reducer