{"version":3,"file":"amplitude.cjs","names":[],"sources":["../src/amplitude.ts"],"sourcesContent":["/**\n * Amplitude Subscriber for autotel\n *\n * Send events to Amplitude for product events.\n *\n * @example\n * ```typescript\n * import { Events } from 'autotel/events';\n * import { AmplitudeSubscriber } from 'autotel-subscribers/amplitude';\n *\n * const events = new Events('checkout', {\n *   subscribers: [\n *     new AmplitudeSubscriber({\n *       apiKey: process.env.AMPLITUDE_API_KEY!\n *     })\n *   ]\n * });\n *\n * events.trackEvent('order.completed', { userId: '123', amount: 99.99 });\n * ```\n */\n\nimport type {\n  EventSubscriber,\n  EventAttributes,\n  FunnelStatus,\n  OutcomeStatus,\n} from 'autotel/event-subscriber';\n\nexport interface AmplitudeConfig {\n  /** Amplitude API key */\n  apiKey: string;\n  /** Enable/disable the subscriber */\n  enabled?: boolean;\n}\n\nexport class AmplitudeSubscriber implements EventSubscriber {\n  readonly name = 'AmplitudeSubscriber';\n  readonly version = '1.0.0';\n\n  private amplitudeModule: any = null;\n  private enabled: boolean;\n  private config: AmplitudeConfig;\n  private initPromise: Promise<void> | null = null;\n\n  constructor(config: AmplitudeConfig) {\n    this.enabled = config.enabled ?? true;\n    this.config = config;\n\n    if (this.enabled) {\n      // Start initialization immediately but don't block constructor\n      this.initPromise = this.initialize();\n    }\n  }\n\n  private async initialize(): Promise<void> {\n    try {\n      // Dynamic import to avoid adding @amplitude/analytics-node as a hard dependency\n      // The SDK exports init(), track(), flush() as separate functions\n      const amplitude = await import('@amplitude/analytics-node');\n      amplitude.init(this.config.apiKey);\n      this.amplitudeModule = amplitude;\n    } catch (error) {\n      console.error(\n        'Amplitude subscriber failed to initialize. Install @amplitude/analytics-node: pnpm add @amplitude/analytics-node',\n        error,\n      );\n      this.enabled = false;\n    }\n  }\n\n  private async ensureInitialized(): Promise<void> {\n    if (this.initPromise) {\n      await this.initPromise;\n      this.initPromise = null;\n    }\n  }\n\n  async trackEvent(name: string, attributes?: EventAttributes): Promise<void> {\n    if (!this.enabled) return;\n\n    await this.ensureInitialized();\n    this.amplitudeModule?.track({\n      event_type: name,\n      user_id: attributes?.userId || attributes?.user_id || 'anonymous',\n      event_properties: attributes,\n    });\n  }\n\n  async trackFunnelStep(\n    funnelName: string,\n    step: FunnelStatus,\n    attributes?: EventAttributes,\n  ): Promise<void> {\n    if (!this.enabled) return;\n\n    await this.ensureInitialized();\n    this.amplitudeModule?.track({\n      event_type: `${funnelName}.${step}`,\n      user_id: attributes?.userId || attributes?.user_id || 'anonymous',\n      event_properties: {\n        funnel: funnelName,\n        step,\n        ...attributes,\n      },\n    });\n  }\n\n  async trackOutcome(\n    operationName: string,\n    outcome: OutcomeStatus,\n    attributes?: EventAttributes,\n  ): Promise<void> {\n    if (!this.enabled) return;\n\n    await this.ensureInitialized();\n    this.amplitudeModule?.track({\n      event_type: `${operationName}.${outcome}`,\n      user_id: attributes?.userId || attributes?.user_id || 'anonymous',\n      event_properties: {\n        operation: operationName,\n        outcome,\n        ...attributes,\n      },\n    });\n  }\n\n  async trackValue(name: string, value: number, attributes?: EventAttributes): Promise<void> {\n    if (!this.enabled) return;\n\n    await this.ensureInitialized();\n    this.amplitudeModule?.track({\n      event_type: name,\n      user_id: attributes?.userId || attributes?.user_id || 'anonymous',\n      event_properties: {\n        value,\n        ...attributes,\n      },\n    });\n  }\n\n  /** Flush pending events before shutdown */\n  async shutdown(): Promise<void> {\n    await this.ensureInitialized();\n    if (this.amplitudeModule) {\n      await this.amplitudeModule.flush();\n    }\n  }\n}\n\n"],"mappings":";;;;AAoCA,IAAa,sBAAb,MAA4D;CAC1D,AAAS,OAAO;CAChB,AAAS,UAAU;CAEnB,AAAQ,kBAAuB;CAC/B,AAAQ;CACR,AAAQ;CACR,AAAQ,cAAoC;CAE5C,YAAY,QAAyB;EACnC,KAAK,UAAU,OAAO,WAAW;EACjC,KAAK,SAAS;EAEd,IAAI,KAAK,SAEP,KAAK,cAAc,KAAK,WAAW;CAEvC;CAEA,MAAc,aAA4B;EACxC,IAAI;GAGF,MAAM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