import { api } from '@/services/api'
import { configureStore } from '@reduxjs/toolkit'
import filterReducer from '@/lib/slice/filter'
import { FLUSH, PAUSE, PERSIST, persistReducer, persistStore, PURGE, REGISTER, REHYDRATE } from "redux-persist";
import { authSlice } from './slice/auth';
import localeReducer from './slice/locale';
import storage from './storage';

const persistAuth = persistReducer({
  key: 'auth',
  storage: storage,
}, authSlice.reducer)

const persistLocale = persistReducer({
  key: 'locale',
  storage: storage,
}, localeReducer)

export const store = configureStore({
  reducer: {
      [api.reducerPath]: api.reducer,
      auth: persistAuth,
      locale: persistLocale,
      filter: filterReducer,
  },
  middleware: (getDefaultMiddleware) =>
      getDefaultMiddleware({
          serializableCheck: {
              ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
          },
      }).concat(api.middleware),
})
  

export const persistor = persistStore(store)
// Infer the type of makeStore
export type AppStore = typeof store
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<AppStore['getState']>
export type AppDispatch = AppStore['dispatch']