import { combineReducers } from 'redux'
import { routerReducer as router } from 'react-router-redux'

// Fix: "React-Redux: Combining reducers: Unexpected Keys"
// http://stackoverflow.com/a/33678198/789076
const initialReducers = {
  home: (state = require('../routes/Home/modules/home').initialState) => state,
  archives: (state = require('../routes/Archives/modules/archives').initialState) => state,
  tags: (state = require('../routes/Tags/modules/tags').initialState) => state,
  post: (state = require('../routes/Article/modules/article').initialState) => state,
  list: (state = require('../routes/Manager/modules/manager').initialState) => state,
  edit: (state = require('../routes/Edit/modules/edit').initialState) => state,
  login: (state = require('../routes/OverView/modules/overview').initialState) => state
}

export const makeRootReducer = (asyncReducers) => {
  return combineReducers({
    // Add sync reducers here
    router,
    ...initialReducers,
    ...asyncReducers
  })
}

export const injectReducer = (store, { key, reducer }) => {
  store.asyncReducers[key] = reducer
  store.replaceReducer(makeRootReducer(store.asyncReducers))
}

export default makeRootReducer
