import type { Action } from 'redux';
import { createEpicMiddleware } from 'redux-observable';
import { configureStore } from '@reduxjs/toolkit';

import { setExternalStore } from '@/modules/@common/store/index';
import rootEpic from '../../../rootEpics';
import rootReducer from '../../../rootReducer';

export type RootState = ReturnType<typeof rootReducer>;

export const epicMiddleware = createEpicMiddleware<Action, Action, RootState>();

const store = configureStore({
  reducer: rootReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      // todo: must be removed when project migrate to slices instead of reducers
      immutableCheck: false,
      serializableCheck: false,
    }).concat(epicMiddleware),
  devTools: true,
});

epicMiddleware.run(rootEpic);

setExternalStore(store);

export type StoreType = typeof store;
export type AppDispatch = typeof store.dispatch;
