{"size": 1916, "ext": "js", "max_stars_repo_path": "src/redux/configureStore.js", "max_stars_repo_name": "atorov/sample-app-2", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/redux/configureStore.js", "max_issues_repo_name": "atorov/sample-app-2", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/redux/configureStore.js", "max_forks_repo_name": "atorov/sample-app-2", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.0843373494, "max_line_length": 71, "alphanum_fraction": 0.5532359081}
import throttle from 'lodash/throttle'

import {
    applyMiddleware,
    combineReducers,
    compose,
    createStore,
} from 'redux'
import { createLogger } from 'redux-logger'
import thunk from 'redux-thunk'

import * as persistState from './persist-state'
import * as reducers from './reducers'

import { isInDevMode } from '../lib/mode-helpers'

function filterState(
    state = {},
    include = [],
    exclude = [],
) {
    let filteredState = {}

    if (include.length) {
        filteredState = include.reduce(
            (acc, key) => ({ ...acc, [key]: state[key] }),
            {},
        )
    } else {
        filteredState = { ...state }
    }

    if (exclude.length) {
        exclude.forEach(key => delete filteredState[key])
    }

    return filteredState
}

export default function (cfg = {}) {
    const combinedReducers = combineReducers(reducers)

    let persistedState = {}
    if (cfg.persist) {
        persistedState = persistState.loadState()
        if (isInDevMode()) {
            console.log('::: persistedState:', persistedState)
        }
    }

    let composeEnhancers = compose
    if (isInDevMode() && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
        composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    }
    const middlewares = [thunk]
    if (cfg.logger) {
        middlewares.push(createLogger())
    }

    const store = createStore(
        combinedReducers,
        persistedState,
        composeEnhancers(applyMiddleware(...middlewares)),
    )

    if (cfg.persist) {
        store.subscribe(
            throttle(
                () => persistState.saveState(
                    filterState(
                        store.getState(),
                        cfg.include,
                        cfg.exclude,
                    ),
                ),
                cfg.throttle || 550,
            ),
        )
    }

    return store
}
