import {createSingletonHistory, MyHistoryOptions} from "@gongt/ts-stl-library/browser/history-object";
import {GlobalVariable} from "@gongt/ts-stl-library/pattern/global-page-data";
import * as React from "react";
import {Router, RouterProps} from "react-router";
import {ReactRender} from "./render";

export function reactUseRouter(react: ReactRender, options: MyHistoryOptions = {}) {
	const preventDuplicate = new GlobalVariable(react);
	if (preventDuplicate.has('router')) {
		throw new TypeError('reactUseRouter(): duplicate call to one react instance.')
	}
	preventDuplicate.set('router', options);
	
	react.wrapComponent<RouterProps>('Router', Router, (global: GlobalVariable) => {
		return {
			history: createSingletonHistory(global, options),
		};
	});
}
