// // --------------- LIBRARIES ---------------
import {configureStore} from '@reduxjs/toolkit';
import createSagaMiddleware from 'redux-saga';
import logger from 'redux-logger';

// // --------------- ASSETS ---------------
import rootReducer from './slices';
import rootSaga from './sagas';
import {TypedUseSelectorHook, useDispatch, useSelector} from 'react-redux';

// Middlewares setup
const sagaMiddleware = createSagaMiddleware();



// const middlewares = [];
// // if (__DEV__) {
// //   middlewares.push(sagaMiddleware);
// //   // middlewares.push(sagaMiddleware, logger); // With logger
// // } else {
//   middlewares.push(sagaMiddleware); // without logger
// // }


const store = configureStore({
  reducer: rootReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({ thunk: false })
      .concat(sagaMiddleware)
      .concat(logger),
  devTools: process.env.NODE_ENV !== 'production',
});

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>;
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch;

export const useAppDispatch: () => AppDispatch = useDispatch;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
// // PersistStore contains all the data from store ----->>>>>
// export const Persistor = persistStore(Store);
sagaMiddleware.run(rootSaga); // Run Saga

export default store;
