import { createStore, applyMiddleware, compose } from 'redux'
import thunkMiddleware from 'redux-thunk'

import { loadState, saveState } from 'shared/utils/localStorage'
import reduxSearchEnhancer from './reduxSearchEnhancer'


const env = process.env.NODE_ENV || 'development'
console.log('ENV: ', env)

const PERSISTING_STATE = [
  'projectsTableState', 'familyTableState', 'savedVariantTableState', 'variantSearchDisplay', 'searchesByHash',
]

const persistStoreMiddleware = store => next => (action) => {
  const result = next(action)
  const nextState = store.getState()
  PERSISTING_STATE.forEach((key) => { saveState(key, nextState[key]) })
  return result
}

// const enhancer = compose(
//   applyMiddleware(thunkMiddleware, persistStoreMiddleware),
//   reduxSearchEnhancer,
// )
//
//
// /**
//  * Initialize the Redux store
//  * @param rootReducer
//  * @param initialState
//  * @returns {*}
//  */
// export const configureStore = (
//   rootReducer = state => state,
//   initialState = {},
// ) => {
//
//   PERSISTING_STATE.forEach((key) => { initialState[key] = loadState(key) })
//
//   console.log('Creating store with initial state:')
//   console.log(initialState)
//
//   return createStore(rootReducer, initialState, enhancer)
// }

/* eslint-disable no-underscore-dangle */
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
const enhancer = composeEnhancers(
  applyMiddleware(thunkMiddleware, persistStoreMiddleware),
  reduxSearchEnhancer,
)
/* eslint-enable */

/**
 * Initialize the Redux store
 * @param rootReducer
 * @param initialState
 * @returns {*}
 */
export const configureStore = (
  rootReducer = state => state,
  initialState = {},
) => {

  PERSISTING_STATE.forEach((key) => { initialState[key] = loadState(key) })

  console.log('Creating store with initial state:')
  console.log(initialState)

  return createStore(rootReducer, initialState, /* preloadedState, */ enhancer)
}


@@ -660,19 +660,16 @@
 sult%0A%7D%0A%0A
-// 
 const en
@@ -690,727 +690,8 @@
 se(%0A
-//   applyMiddleware(thunkMiddleware, persistStoreMiddleware),%0A//   reduxSearchEnhancer,%0A// )%0A//%0A//%0A// /**%0A//  * Initialize the Redux store%0A//  * @param rootReducer%0A//  * @param initialState%0A//  * @returns %7B*%7D%0A//  */%0A// export const configureStore = (%0A//   rootReducer = state =%3E state,%0A//   initialState = %7B%7D,%0A// ) =%3E %7B%0A//%0A//   PERSISTING_STATE.forEach((key) =%3E %7B initialState%5Bkey%5D = loadState(key) %7D)%0A//%0A//   console.log('Creating store with initial state:')%0A//   console.log(initialState)%0A//%0A//   return createStore(rootReducer, initialState, enhancer)%0A// %7D%0A%0A/* eslint-disable no-underscore-dangle */%0Aconst composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ %7C%7C compose%0Aconst enhancer = composeEnhancers(%0A
   ap
@@ -775,28 +775,9 @@
 ,%0A)%0A
-/* eslint-enable */
 %0A
+
 %0A/**
@@ -1174,30 +1174,8 @@
 ate,
- /* preloadedState, */
  enh
@@ -1183,9 +1183,8 @@
 ncer)%0A%7D%0A
-%0A

remove redux dev tool comment