import { createStore, applyMiddleware, compose, Middleware, Store } from 'redux';
import { thunk } from 'redux-thunk';

import { IState } from '@/types/store';
import { isDev } from '@/utils/common';

import reducers from './reducers';

const composeEnhancers =
  typeof window === 'object' && (window as BL.Anything).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    ? (window as BL.Anything).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
        // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
      })
    : compose;

const middlewares: Middleware[] = [thunk];

if (isDev()) {
  // eslint-disable-next-line @typescript-eslint/no-var-requires
  middlewares.push(require('redux-logger').createLogger());
}

const enhancer = composeEnhancers(applyMiddleware(...middlewares));

function configStore() {
  const store = createStore(reducers, enhancer);
  return store as Store<IState>;
}

export default configStore();
