import { createStore, compose, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import reducers from "./reducers/reducers";
import { saveState } from "./persisted.store.cookies.js";

export default function configureStore(initialState) {
	// const enhancers = compose(typeof window !== "undefined" && window.devToolsExtension ? window.devToolsExtension() : (f) => f);
	const composeEnhancers = (typeof window !== "undefined" && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || compose;
	const store = createStore(
		reducers,
		{
			...initialState,
		},
		composeEnhancers(applyMiddleware(thunk))
		// enhancers
	);

	// add a listener that will be invoked on any state change
	store.subscribe(() => {
		saveState(store.getState());
	});

	return store;
}
