import { getComponentId } from '../utils/utils' ; enum MutationType { VALUE = 1 , ATTR = 2 , REMOVE_ATTR = 3 , REMOVE = 4 , MOVE = 5 , INSERT = 6 } interface DominoMutationEvent { type : MutationType ; target : HTMLElement & { [ k ] : Function | undefined ; } ; node ? : HTMLElement & { [ k ] : Function | undefined ; } ; attr ? : { data : string ; name : string ; } ; } const START_COMMENT = '__start__' ; function createServerRenderer ( doc , compId ? ) { let commentIndex = 0 ; return { createComment : ( data ) => { const startComment = doc . createComment ( 's' + commentIndex . toString ( 16 ) ) ; const endComment = doc . createComment ( 'e' + commentIndex . toString ( 16 ) ) ; ( endComment as any ) [ START_COMMENT ] = startComment ; commentIndex ++ ; return endComment ; } , createElement : ( tag ) => { const el = doc . createElement ( tag ) ; if ( compId != null ) { el . setAttribute ( `_ngcontent-${ compId }` , '' ) ; } return el ; } , createElementNS : ( namespace , tag ) => doc . createElementNS ( namespace , tag ) as any , createTextNode : ( data ) => doc . createTextNode ( data ) , querySelector : ( selectors ) => doc . querySelector ( selectors ) as any , } ; } ; export function getRendererFactory ( doc , scoped ) { return { createRenderer : ( hostElement , rendererType ) => { ( doc as any ) . __current_element__ = hostElement ; const compId = ( hostElement && scoped ) ? getComponentId ( doc , hostElement . localName ) : undefined ; if ( hostElement ) { hostElement . setAttribute ( '_s' , '' ) ; if ( scoped ) { hostElement . setAttribute ( `_nghost-${ compId }` , '' ) ; } } const oldMutationHandler = ( doc as any ) . mutationHandler ; ( doc as any ) . _setMutationHandler ( ( event ) => { const target = event . target ; const node = event . node ! ; switch ( event . type ) { case MutationType . INSERT : if ( ( node as any ) [ START_COMMENT ] != null ) { target . insertBefore ( ( node as any ) [ START_COMMENT ] , node ) ; } break ; } if ( oldMutationHandler ) { oldMutationHandler ( event ) ; } } ) ; return createServerRenderer ( doc , compId ) ; } } ; }