import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';
import createHelpers from './createHelpers';

import { routerMiddleware } from 'react-router-redux';

export default function configureStore(history, initialState, helpersConfig) {
  const helpers = createHelpers(helpersConfig);
  const middleware = [thunk.withExtraArgument(helpers), routerMiddleware(history)];

  let enhancer;

  if (__DEV__) {
    if (process.env.BROWSER) {
      const createLogger = require('redux-logger');
      middleware.push(createLogger({
        collapsed: true,
      }));
    } else {
      // Server side redux action logger
      middleware.push(store => next => action => { // eslint-disable-line no-unused-vars
        const payload = JSON.stringify(action.payload);
        console.log(` * ${action.type}: ${payload}`); // eslint-disable-line no-console
        return next(action);
      });
    }

    // https://github.com/zalmoxisus/redux-devtools-extension#redux-devtools-extension
    let devToolsExtension = f => f;
    if (process.env.BROWSER && window.devToolsExtension) {
      devToolsExtension = window.devToolsExtension();
    }

    enhancer = compose(
      applyMiddleware(...middleware),
      devToolsExtension,
    );
  } else {
    enhancer = applyMiddleware(...middleware);
  }

  // See https://github.com/rackt/redux/releases/tag/v3.1.0
  const store = createStore(rootReducer, initialState, enhancer);

  // Hot reload reducers (requires Webpack or Browserify HMR to be enabled)
  if (__DEV__ && module.hot) {
    module.hot.accept('../reducers', () =>
      store.replaceReducer(require('../reducers').default)
    );
  }

  return store;
}
