/**
 * @flow
 */
//import _ from 'lodash'
import devTools from 'remote-redux-devtools';
import { Platform, AsyncStorage } from 'react-native';
import {
  persistStore,
  persistCombineReducers,
} from 'redux-persist';

import { createStore, applyMiddleware, compose } from 'redux';
import {
  createReactNavigationReduxMiddleware,
} from 'react-navigation-redux-helpers';

import middleWare from './utils/middleware';
import { markUpdateSuccess } from './utils/update';


import AppReducer from './reducers';


function setConfig() {
  if (__DEV__) {
    return {
      key: 'root',
      storage: AsyncStorage,
      blacklist: ['loading'],
    }; //'nav','theme'
  }
  return {
    key: 'root',
    storage: AsyncStorage,
    whitelist: [
      'theme',
      'systemAPPStore',
    ],
  };
}

const config = setConfig()

const appReducer = persistCombineReducers(config, AppReducer)


const middleware = createReactNavigationReduxMiddleware(
  state => state.nav,
)

function createAppStore(reducer) {
  /* eslint-disable */
  const devEnhancer = devTools({
    name: Platform.OS,
    hostname: 'localhost',
    port: 5678,
  })
  const productionEnhancer = compose(middleWare,applyMiddleware(middleware))
  const fn='__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'
  let devMac= window[fn] ? window[fn]():devEnhancer
  //console.log('devMac:', devMac)
  //devMac 和enhancer 二选一,如果使用和enhancer,  package.json 里面postinstall 脚本块需要替换成postinstall-devTools
  const enhancer = __DEV__ ? compose(productionEnhancer, devMac /* enhancer */) : productionEnhancer
    /* eslint-enabled */
  const store = createStore(
    reducer,
undefined,
    enhancer
  )
return store
}

function configureStore() {
  let store = createAppStore(appReducer);
  let persistor = persistStore(store,null,onStoreInit);
  return { persistor, store }
}

function onStoreInit (){
  if(markUpdateSuccess) {
    markUpdateSuccess()
  }
}

export default configureStore
