import { applyMiddleware, combineReducers, compose, createStore } from "redux";
import createSagaMiddleware from "redux-saga";
import { fork } from "redux-saga/effects";
import { ERROR_NAMESPACE, errorReducer } from "./errors";
import { HIRO_NAMESPACE } from "./constants";
import { GRAPH_NAMESPACE, handleGraphRequests, graphReducer } from "./graph";
// Combine built-in sagas
function* mainSaga(orm) {
    yield fork(handleGraphRequests, orm);
}
export function init({ state, reducers, sagas, orm } = {}) {
    // Combine built-in reducers with exeternal ones
    const rootReducer = combineReducers({
        [HIRO_NAMESPACE]: combineReducers({
            [ERROR_NAMESPACE]: errorReducer,
            [GRAPH_NAMESPACE]: graphReducer
        }),
        ...reducers
    });
    const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
    const sagaMiddleware = createSagaMiddleware();
    // Create store
    const store = createStore(rootReducer, state, 
    /* preloadedState, */
    composeEnhancers(applyMiddleware(sagaMiddleware)));
    // Run built-in saga
    sagaMiddleware.run(mainSaga, orm);
    // Run external sagas
    if (sagas) {
        for (const saga of sagas) {
            sagaMiddleware.run(saga);
        }
    }
    return store;
}
