import type { SegmentClient } from './analytics' ; import { Timeline } from './timeline' ; import { AliasEventType , EventType , GroupEventType , IdentifyEventType , PluginType , ScreenEventType , SegmentAPISettings , SegmentEvent , TrackEventType , UpdateType , } from './types' ; export class Plugin { type : PluginType = PluginType . utility ; analytics ? : SegmentClient = undefined ; configure ( analytics ) { this . analytics = analytics ; } update ( settings , type ) { } execute ( event ) { return event ; } shutdown ( ) { } } export class EventPlugin extends Plugin { execute ( event ) { if ( event === undefined ) { return event ; } let result = event ; switch ( result . type ) { case EventType . IdentifyEvent : result = this . identify ( result ) ; break ; case EventType . TrackEvent : result = this . track ( result ) ; break ; case EventType . ScreenEvent : result = this . screen ( result ) ; break ; case EventType . AliasEvent : result = this . alias ( result ) ; break ; case EventType . GroupEvent : result = this . group ( result ) ; break ; } return result ; } identify ( event ) { return event ; } track ( event ) { return event ; } screen ( event ) { return event ; } alias ( event ) { return event ; } group ( event ) { return event ; } flush ( ) { } reset ( ) { } } export class DestinationPlugin extends EventPlugin { type = PluginType . destination ; key = '' ; timeline = new Timeline ( ) ; /**
       Adds a new plugin to the currently loaded set.
  
       - Parameter plugin: The plugin to be added.
       - Returns: Returns the name of the supplied plugin.
    */ add ( plugin ) { const analytics = this . analytics ; if ( analytics ) { plugin . configure ( analytics ) ; } this . timeline . add ( plugin ) ; return plugin ; } /**
       Applies the supplied closure to the currently loaded set of plugins.
  
       - Parameter closure: A closure that takes an plugin to be operated on as a parameter.
    */ apply ( closure ) { this . timeline . apply ( closure ) ; } configure ( analytics ) { this . analytics = analytics ; this . apply ( ( plugin ) => { plugin . configure ( analytics ) ; } ) ; } /**
       Removes and unloads plugins with a matching name from the system.
  
       - Parameter pluginName: An plugin name.
    */ remove ( plugin ) { this . timeline . remove ( plugin ) ; } } export class UtilityPlugin extends EventPlugin { } export class PlatformPlugin extends Plugin { }