import { legacy_createStore as createStore, applyMiddleware, compose } from "redux";
import { persistStore } from "redux-persist";
import { createLogger } from "redux-logger";
import createSagaMiddleware from "redux-saga";

import rootReducer from "./root.reducer";
import rootSaga from "./root.saga";

const sagaMiddleWare = createSagaMiddleware();

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

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);

const epxortObj = { store, persistStore };
export default epxortObj;
