import {reduxHandleReactRouterBase} from "@gongt/ts-stl-client/redux/router/redux-router";
import {createSingletonHistory, MyHistoryOptions} from "@gongt/ts-stl-library/browser/history-object";
import {GlobalVariable} from "@gongt/ts-stl-library/pattern/global-page-data";
import {Request} from "express-serve-static-core";
import * as React from "react";
import {ReactRender} from "./react";
import {reactUseRedux} from "./react.redux";
import {ReduxStoreExpress} from "./redux";

export function reduxHandleReactRouter(react: ReactRender,
                                       redux: ReduxStoreExpress<any>,
                                       options: MyHistoryOptions = {},) {
	const getOption = (global: GlobalVariable) => {
		const req: Request = global.parentObject.req;
		return Object.assign({}, options, {
			current: req.originalUrl,
		});
	};
	redux.addPreloadState((request, global, pl: any) => {
		const history = createSingletonHistory(global, getOption(global));
		pl.routing = {
			location: history.location,
		};
	});
	reduxHandleReactRouterBase(react, redux, getOption);
	reactUseRedux(react, redux);
}
