import { configureStore } from '@reduxjs/toolkit';
import { api, authSlice, cartSlice } from '../../api/api';
import storage from 'redux-persist/lib/storage';
import { combineReducers } from 'redux';

import {
  persistReducer, persistCombineReducers, persistStore, FLUSH,
  REHYDRATE,
  PAUSE,
  PERSIST,
  PURGE,
  REGISTER,
} from 'redux-persist';

const persistConfig = {
  key: 'all',
  storage,
  blacklist: [api.reducerPath] //стейт каких редьюсеров не хранить в localstorage
};

const persistedReducer = persistCombineReducers(
  persistConfig,
  {
    [api.reducerPath]: api.reducer,
    [authSlice.name]: authSlice.reducer,
    [cartSlice.name]: cartSlice.reducer,
  });


export const store = configureStore({
  reducer: persistedReducer,//это combineReducers
  middleware: (getDefaultMiddleware) =>
    [...getDefaultMiddleware({ serializableCheck: { ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER] } }),
    api.middleware],
});

const persistor = persistStore(store);

export default store;