import { AsyncStorage } from 'react-native';
import { applyMiddleware, createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import { createLogicMiddleware } from 'redux-logic';
import { autoRehydrate, persistStore } from 'redux-persist';

import { logic } from '../logic';
import { rootReducer } from '../reducers';
declare let module: any;

const logicDeps = {
};

export function configureStore() {
  const logicMiddleware = createLogicMiddleware(logic, logicDeps);

  const enhancer = composeWithDevTools(
    applyMiddleware(logicMiddleware),
    autoRehydrate(),
  );

  const store = createStore(
    rootReducer,
    enhancer,
  );

  const persistConfig = {
    storage: AsyncStorage,
    blacklist: ['network', 'navigation'],
  };

  persistStore(store, persistConfig);

  if (module.hot) {
    module.hot.accept(() => {
      const nextRootReducer = require('../reducers/index').default;
      store.replaceReducer(nextRootReducer);
    });
  }
  return store;
}
