import { configureStore } from '@reduxjs/toolkit';
import { combineReducers } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // Utilisez localStorage par défaut

import clientReducer from "./features/client/slice";
import articleReducer from "./features/article/slice";
import devisReducer from "./features/devis/slice";
import commandeReducer from "./features/commande/slice";
import factureReducer from "./features/factures/slice";
import BLReducer from "./features/bl/slice";
import fournisseurReducer from "./features/fournisseur/slice";
import avoirReducer from "./features/avoir/slice";
import familleReducer from "./features/famille/slice";
import contactReducer from "./features/contact/slice";
import commercialsReducer from "./features/commercial/slice"
import gatewaysReducer from "./features/gateways/slice";
import universignReducer from "./features/universign/slice";
import userReducer from "./features/user/slice";
import entrepriseReducer from "./features/entreprise/slice"
import sageBuilderReducer from "./features/sage-builder/slice";
import reglementReducer from "./features/reglement/slice";
import askIaReducer from "./features/ask-ia/slice";

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


const appReducer = combineReducers({
  client: clientReducer,
  article: articleReducer,
  devis: devisReducer,
  commande: commandeReducer,
  facture: factureReducer,
  BL: BLReducer,
  fournisseur: fournisseurReducer,
  avoir: avoirReducer,
  famille: familleReducer,
  contact: contactReducer,
  gateways: gatewaysReducer,
  commercials: commercialsReducer,
  universign: universignReducer,
  user: userReducer,
  entreprise: entrepriseReducer,
  sageBuilder: sageBuilderReducer,
  reglement: reglementReducer,
  askIa: askIaReducer,
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const rootReducer = (state: any, action: any) => {
  if (action.type === 'RESET_APP') {
    state = undefined;
  }
  return appReducer(state, action);
};


const persistConfig = {
  key: 'root',
  storage,
};

const persistedReducer = persistReducer(persistConfig, rootReducer);

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

export const persistor = persistStore(store);


export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;



