import { createStore, combineReducers, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import { boardReducer } from '../reducers/boardReducer'
import { listsReducer } from '../reducers/listsReducer'
import { cardsReducer } from '../reducers/cardsReducer'
import { activityReducer } from '../reducers/activityReducer'
import { imageReducer } from '../reducers/imageReducer'
import { userReducer } from '../reducers/userReducer'
import * as ACTIONS from '../actions/actions'

const appReducer = combineReducers({
  boards: boardReducer,
  lists: listsReducer,
  cards: cardsReducer,
  activities: activityReducer,
  images: imageReducer,
  user: userReducer,
})

const rootReducer = (state, action) => {
  if (action.type === ACTIONS.LOGOUT_USER) {
    // eslint-disable-next-line no-param-reassign
    state = undefined
  }
  return appReducer(state, action)
}

export default createStore(
  rootReducer,
  compose(
    applyMiddleware(thunk),
    // window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  ),
)
