import React from 'react' ; const generateKey = ( ) => Math . random ( ) . toString ( 36 ) . substr ( 2 , 6 ) ; export type WindowInternal = Window & { CustomEvent ? : new < T > ( typeArg , eventInitDict ? ) => CustomEvent < T > ; Event : typeof Event ; } ; type IPrototype = { prototype : any ; } ; type InitCustomEventParams < T = any > = { bubbles : boolean ; cancelable : boolean ; detail : T ; } ; export const CreateEvent = ( windowInternal , documentInternal ) => ( event , params ) : CustomEvent => { if ( typeof windowInternal . CustomEvent === 'function' ) { return new windowInternal . CustomEvent ( event , params ) ; } const paramsToFunction = params || { bubbles : false , cancelable : false , detail : undefined } ; const evt = documentInternal . createEvent ( 'CustomEvent' ) ; evt . initCustomEvent ( event , paramsToFunction . bubbles , paramsToFunction . cancelable , paramsToFunction . detail ) ; ( evt as CustomEvent & IPrototype ) . prototype = windowInternal . Event . prototype ; return evt ; } ; type WindowHistoryState = typeof window . history . state ; export interface ReactOidcHistory { push : ( url ? : string | null , stateHistory ? : WindowHistoryState ) => void ; } const getHistory = ( windowInternal , CreateEventInternal , generateKeyInternal ) => { return { push : ( url ? , stateHistory ? ) : void => { const key = generateKeyInternal ( ) ; const state = stateHistory || windowInternal . history . state ; windowInternal . history . pushState ( { key , state } , null , url ) ; windowInternal . dispatchEvent ( CreateEventInternal ( 'popstate' ) ) ; } , } ; } ; export const useHistory = ( ) => getHistory ( window , CreateEvent ( window , document ) , generateKey ) ; export const withRouter = ( windowInternal , CreateEventInternal , generateKeyInternal ) => ( Component ) => ( props ) => { const oidcHistory = getHistory ( windowInternal , CreateEventInternal , generateKeyInternal ) ; const enhanceProps = { history : oidcHistory , location : windowInternal . location , ... props , } ; return < Component { ... enhanceProps } / > ; } ; export default withRouter ( window , CreateEvent ( window , document ) , generateKey ) ;