import {applyMiddleware, compose, createStore} from 'redux'
import {createLogger} from 'redux-logger'
import {createEpicMiddleware} from 'redux-observable'
import {ReduxState, rootEpics, rootReducers} from './combine'

const epicMiddleware = createEpicMiddleware()

const mWindow: any = window

const isDebuggingInChrome = __DEV__ && !!mWindow.navigator.userAgent
const logger = createLogger({
  predicate: () => isDebuggingInChrome,
  collapsed: true,
  duration: true,
})

const middleware = isDebuggingInChrome ? [epicMiddleware, logger] : [epicMiddleware]

export default function configureStore(initialState?: Partial<ReduxState>) {
  const appliedMiddleware = applyMiddleware(...middleware)

  const store = createStore(
    rootReducers,
    initialState,
    compose(
      appliedMiddleware,
      isDebuggingInChrome && mWindow.devToolsExtension ? mWindow.devToolsExtension() : (f: any) => f,
    ),
  )

  epicMiddleware.run(rootEpics)
  return store
}
