import { Descriptor , identifierKey , RcModuleV2 } from './RcModule' ; type Analytics = any ; type TrackEvent = string | ( ( ... args : any ) => ( [ string , object ? ] | ( ( analytics : Analytics ) => [ string , object ? ] | void ) ) | void ) ; param trackEvent export const track = ( trackEvent ) => { return ( target , name , descriptor ? ) => { if ( typeof descriptor . value !== 'function' && typeof descriptor . initializer !== 'function' ) { throw new Error ( `@track decorated '${ name }' is not a method` ) ; } let fn = descriptor . value ; const initializer = descriptor . initializer ; const trackedFn = function ( this , ... args ) { const { analytics } = this . parentModule as any ; if ( typeof initializer === 'function' ) { fn = initializer . call ( this ) ; } if ( typeof fn !== 'function' ) { throw new Error ( `@track decorated '${ name }' is not a function` ) ; } const result = fn . apply ( this , args ) ; try { if ( typeof analytics ?. track === 'function' ) { if ( typeof trackEvent === 'string' ) { analytics . track ( trackEvent ) ; } else { let trackReturn = trackEvent ( this , ... args ) ; if ( typeof trackReturn === 'function' ) { trackReturn = trackReturn ( analytics ) ; } if ( Array . isArray ( trackReturn ) ) { const [ event , trackProps ] = trackReturn ; if ( event ) { analytics . track ( event , trackProps ) ; } } } } } catch ( e ) { console . warn ( `Analytics Error: ${ target [ identifierKey ] }.${ name }` ) ; console . error ( e ) ; } return result ; } ; return { enumerable : true , configurable : true , value : trackedFn } as any ; } ; } ;