import { configureStore, combineReducers } from "@reduxjs/toolkit";
import cartReducer from './cartSlice'
import authReducer from './authSlice'
import adminMenuReducer from './adminMenuSlice'
import { categoriesApi } from './categoriesApi'
import { ImgUploadApi } from "./ImgUploadApi";
import {
    persistStore,
    persistReducer,
    FLUSH,
    REHYDRATE,
    PAUSE,
    PERSIST,
    PURGE,
    REGISTER,
} from 'redux-persist'
import storage from 'redux-persist/lib/storage' // defaults to localStorage for web

const rootReducer = combineReducers({
    cart: cartReducer,
    auth: authReducer,
    adminMenu: adminMenuReducer,
    [categoriesApi.reducerPath]: categoriesApi.reducer,
    [ImgUploadApi.reducerPath]: ImgUploadApi.reducer,
})

const persistConfig = {
    key: 'root',
    storage,
    blacklist: [categoriesApi.reducerPath, ImgUploadApi.reducerPath]
}

const persistedReducer = persistReducer(persistConfig, rootReducer)

const store = configureStore({
    reducer: persistedReducer,
    middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
      },
    }).concat(categoriesApi.middleware).concat(ImgUploadApi.middleware),
})

export const persistor = persistStore(store)

// export default configureStore({
//     reducer:{
//         cart: cartReducer,
//         auth: authReducer,
//         adminMenu: adminMenuReducer,
//         [categoriesApi.reducerPath]: categoriesApi.reducer,
//         [ImgUploadApi.reducerPath]: ImgUploadApi.reducer,
//     },
//         middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(categoriesApi.middleware).concat(ImgUploadApi.middleware)
// })
export default store
