/*
 * Boileplate code for redux store. Usually, this file should not be modified
 */
import { createStore, applyMiddleware, compose } from 'redux';
import Immutable, { fromJS,  Iterable } from 'immutable';

//import { routerReducer, routerMiddleware } from 'react-router-redux';
import promiseMiddleware from 'redux-promise-middleware';
import { authMiddleware } from 'lib/auth';

import { createIntlReducer } from 'lib/i18n';

import createReducers from './reducers';
import createLogger from 'redux-logger';


export default function configureStore(initialState = {}, history) {

  const middlewares = [
    promiseMiddleware(),
    authMiddleware()
  ];

  if (process.env.NODE_ENV !== "production") {
    middlewares.push(createLogger({
      stateTransformer: state => Iterable.isIterable(state) ? state.toJS() : state
    }));
  }

  const enhancers = [
    applyMiddleware(...middlewares),
  ];

  if (process.env.NODE_ENV !== "production") {
    const devtools = window.devToolsExtension || (() => (noop) => noop);
    enhancers.push(devtools());
  }

  const store = createStore(
    createReducers({ intl: createIntlReducer() }),
    fromJS(initialState),
    compose(...enhancers)
  );


  // Make reducers hot reloadable, see http://mxs.is/googmo
  /*
  if (module.hot) {
    module.hot.accept('./reducers', function(reducerModule) {
        console.log('trying to hot-replace reducers');
        const createReducers = reducerModule.default;
        const nextReducers = createReducers(store.asyncReducers);

        store.replaceReducer(nextReducers);
    });
  }
  */

  return store;
}
