// @flow

import { createStore, applyMiddleware, compose } from 'redux';
import { routerMiddleware } from 'react-router-redux';
import type { Store } from 'redux';
import createReducer from './reducers';

export const injectAsyncReducer = (
  store: AmazeeStore<any, any>,
  name: string,
  asyncReducer: Object,
): void => {
  store.asyncReducers[name] = asyncReducer; // eslint-disable-line
  store.replaceReducer(createReducer(store.apolloClient, store.asyncReducers));
};

const configureStore = (
  apolloClient: any,
  history: any,
  initialState?: any = {},
): AmazeeStore<any, any> => {
  const middlewares: Array<Function> = [
    routerMiddleware(history),
    apolloClient.middleware(),
  ];

  const enhancers: Array<Function> = [applyMiddleware(...middlewares)];

  // @TODO Disable this on production at some point.
  // eslint-disable-next-line no-constant-condition
  if (__DEVELOPMENT__ || true) {
    const devToolsExtension =
      (__CLIENT__ && global.devToolsExtension) ||
      (() => (noop: any): any => noop);

    enhancers.push(devToolsExtension());
  }

  // Make sure that the apollo state is initialized.
  const initialStateFinal = {
    ...initialState,
    apollo: initialState.apollo || {},
  };

  // Create the store with the enhancers.
  const store: Store<any, any> = createStore(
    createReducer(apolloClient),
    initialStateFinal,
    compose(...enhancers),
  );

  // Make reducers hot reloadable.
  if (
    module.hot &&
    module.hot.accept &&
    typeof module.hot.accept === 'function'
  ) {
    module.hot.accept('./reducers', (): void => {
      const nextRootReducer: Object = require('./reducers').default; // eslint-disable-line global-require
      store.replaceReducer(nextRootReducer);
    });
  }

  // Extend the store with asynchronous reducers.
  const extendedStore: AmazeeStore<any, any> = {
    ...store,
    asyncReducers: {},
    apolloClient,
  };

  return extendedStore;
};

export default configureStore;

@@ -775,113 +775,8 @@
 %5D;%0A%0A
-  // @TODO Disable this on production at some point.%0A  // eslint-disable-next-line no-constant-condition%0A
   if
@@ -796,16 +796,8 @@
 NT__
- %7C%7C true
 ) %7B%0A

Disable redux debugger in production.