import { applyMiddleware, compose, createStore } from "redux";
import { persistStore } from "redux-persist";
import { createLogger } from "redux-logger";
import rootSaga from "./root.saga";
import rootReducer from "./root.reducer";
const createSagaMiddleware = require('redux-saga').default;
const sagaMiddleWare = createSagaMiddleware();

const middlewares = [sagaMiddleWare];
if (process.env.NODE_ENV === "development") {
  middlewares.push(
    createLogger({
      collapsed: true,
    })
  );
}

//Add in for React Native Debugger.
const composeEnhancers =
  typeof window === "object" && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
      // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
    })
    : compose;

const enhancer = composeEnhancers(
  applyMiddleware(...middlewares)
  // other store enhancers if any
);

export const store = createStore(rootReducer, enhancer);
sagaMiddleWare.run(rootSaga);

export const persistor = persistStore(store);

export default { store };
