import type * as d from '../declarations' ; import { CMP_FLAGS , LISTENER_FLAGS , MEMBER_FLAGS } from './constants' ; export const formatLazyBundleRuntimeMeta = ( bundleId , cmps ) : d . LazyBundleRuntimeData => { return [ bundleId , cmps . map ( ( cmp ) => formatComponentRuntimeMeta ( cmp , true ) ) ] ; } ; export const formatComponentRuntimeMeta = ( compilerMeta , includeMethods ) : d . ComponentRuntimeMetaCompact => { let flags = 0 ; if ( compilerMeta . encapsulation === 'shadow' ) { flags |= CMP_FLAGS . shadowDomEncapsulation ; if ( compilerMeta . shadowDelegatesFocus ) { flags |= CMP_FLAGS . shadowDelegatesFocus ; } } else if ( compilerMeta . encapsulation === 'scoped' ) { flags |= CMP_FLAGS . scopedCssEncapsulation ; } if ( compilerMeta . encapsulation !== 'shadow' && compilerMeta . htmlTagNames . includes ( 'slot' ) ) { flags |= CMP_FLAGS . hasSlotRelocation ; } if ( compilerMeta . hasMode ) { flags |= CMP_FLAGS . hasMode ; } const members = formatComponentRuntimeMembers ( compilerMeta , includeMethods ) ; const hostListeners = formatHostListeners ( compilerMeta ) ; return trimFalsy ( [ flags , compilerMeta . tagName , Object . keys ( members ) . length > 0 ? members : undefined , hostListeners . length > 0 ? hostListeners : undefined , ] ) ; } ; export const stringifyRuntimeData = ( data ) => { const json = JSON . stringify ( data ) ; if ( json . length > 10000 ) { return `JSON.parse(${ JSON . stringify ( json ) })` ; } return json ; } ; const formatComponentRuntimeMembers = ( compilerMeta , includeMethods = true ) : d . ComponentRuntimeMembers => { return { ... formatPropertiesRuntimeMember ( compilerMeta . properties ) , ... formatStatesRuntimeMember ( compilerMeta . states ) , ... ( includeMethods ? formatMethodsRuntimeMember ( compilerMeta . methods ) : { } ) , } ; } ; const formatPropertiesRuntimeMember = ( properties ) => { const runtimeMembers = { } ; properties . forEach ( ( member ) => { runtimeMembers [ member . name ] = trimFalsy ( [ formatFlags ( member ) , formatAttrName ( member ) , ] ) ; } ) ; return runtimeMembers ; } ; const formatFlags = ( compilerProperty ) => { let type = formatPropType ( compilerProperty . type ) ; if ( compilerProperty . mutable ) { type |= MEMBER_FLAGS . Mutable ; } if ( compilerProperty . reflect ) { type |= MEMBER_FLAGS . ReflectAttr ; } return type ; } ; const formatAttrName = ( compilerProperty ) => { if ( typeof compilerProperty . attribute === 'string' ) { if ( compilerProperty . name === compilerProperty . attribute ) { return undefined ; } return compilerProperty . attribute ; } return undefined ; } ; const formatPropType = ( type ) => { if ( type === 'string' ) { return MEMBER_FLAGS . String ; } if ( type === 'number' ) { return MEMBER_FLAGS . Number ; } if ( type === 'boolean' ) { return MEMBER_FLAGS . Boolean ; } if ( type === 'any' ) { return MEMBER_FLAGS . Any ; } return MEMBER_FLAGS . Unknown ; } ; const formatStatesRuntimeMember = ( states ) => { const runtimeMembers = { } ; states . forEach ( ( member ) => { runtimeMembers [ member . name ] = [ MEMBER_FLAGS . State , ] ; } ) ; return runtimeMembers ; } ; const formatMethodsRuntimeMember = ( methods ) => { const runtimeMembers = { } ; methods . forEach ( ( member ) => { runtimeMembers [ member . name ] = [ MEMBER_FLAGS . Method , ] ; } ) ; return runtimeMembers ; } ; const formatHostListeners = ( compilerMeta ) => { return compilerMeta . listeners . map ( ( compilerListener ) => { const hostListener = [ computeListenerFlags ( compilerListener ) , compilerListener . name , compilerListener . method , ] ; return hostListener ; } ) ; } ; const computeListenerFlags = ( listener ) => { let flags = 0 ; if ( listener . capture ) { flags |= LISTENER_FLAGS . Capture ; } if ( listener . passive ) { flags |= LISTENER_FLAGS . Passive ; } switch ( listener . target ) { case 'document' : flags |= LISTENER_FLAGS . TargetDocument ; break ; case 'window' : flags |= LISTENER_FLAGS . TargetWindow ; break ; case 'body' : flags |= LISTENER_FLAGS . TargetBody ; break ; case 'parent' as any : flags |= LISTENER_FLAGS . TargetParent ; break ; } return flags ; } ; const trimFalsy = ( data ) : any => { const arr = data as any [ ] ; for ( var i = arr . length - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] ) { break ; } arr . pop ( ) ; } return arr ; } ;