import { createStore, applyMiddleware, combineReducers } from 'redux';
import createLogger from 'redux-logger';
import rootReducer from './reducers';
import thunk from 'redux-thunk';
import apiMiddleware from './utils/apiMiddleware';

var logger = createLogger({
  predicate(getState, action) {
    //don't log api middleware action
    if (action.types) {
      return false;
    }
    return true;
  }
});


const createStoreWithMiddleware = applyMiddleware(
  thunk,
  logger,
  apiMiddleware
)(createStore);

export default function configureStore(initialState) {
  const store = createStoreWithMiddleware(rootReducer, initialState);

  //support hot loading as per https://github.com/rackt/react-redux/releases/tag/v2.0.0
  if (module.hot) {
    module.hot.accept('./reducers', () => {
      const nextRootReducer = require('./reducers');
      store.replaceReducer(nextRootReducer);
    });
  }

  return store;
}
