{"version":3,"file":"segment.cjs","names":[],"sources":["../src/segment.ts"],"sourcesContent":["/**\n * Segment Subscriber for autotel\n *\n * Send events to Segment (customer data platform).\n *\n * @example\n * ```typescript\n * import { Events } from 'autotel/events';\n * import { SegmentSubscriber } from 'autotel-subscribers/segment';\n *\n * const events = new Events('checkout', {\n *   subscribers: [\n *     new SegmentSubscriber({\n *       writeKey: process.env.SEGMENT_WRITE_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 SegmentConfig {\n  /** Segment write key */\n  writeKey: string;\n  /** Enable/disable the subscriber */\n  enabled?: boolean;\n}\n\nexport class SegmentSubscriber implements EventSubscriber {\n  readonly name = 'SegmentSubscriber';\n  readonly version = '1.0.0';\n\n  private events: any;\n  private enabled: boolean;\n  private config: SegmentConfig;\n  private initPromise: Promise<void> | null = null;\n\n  constructor(config: SegmentConfig) {\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 @segment/events-node as a hard dependency\n      const { Analytics } = await import('@segment/analytics-node');\n      this.events = new Analytics({ writeKey: this.config.writeKey });\n    } catch (error) {\n      console.error(\n        'Segment subscriber failed to initialize. Install @segment/events-node: pnpm add @segment/events-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.events?.track({\n      userId: attributes?.userId || attributes?.user_id || 'anonymous',\n      event: name,\n      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.events?.track({\n      userId: attributes?.userId || attributes?.user_id || 'anonymous',\n      event: `${funnelName}.${step}`,\n      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.events?.track({\n      userId: attributes?.userId || attributes?.user_id || 'anonymous',\n      event: `${operationName}.${outcome}`,\n      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.events?.track({\n      userId: attributes?.userId || attributes?.user_id || 'anonymous',\n      event: name,\n      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.events) {\n      await this.events.closeAndFlush();\n    }\n  }\n}\n\n"],"mappings":";;;;AAoCA,IAAa,oBAAb,MAA0D;CACxD,AAAS,OAAO;CAChB,AAAS,UAAU;CAEnB,AAAQ;CACR,AAAQ;CACR,AAAQ;CACR,AAAQ,cAAoC;CAE5C,YAAY,QAAuB;EACjC,KAAK,UAAU,OAAO,WAAW;EACjC,KAAK,SAAS;EAEd,IAAI,KAAK,SAEP,KAAK,cAAc,KAAK,WAAW;CAEvC;CAEA,MAAc,aAA4B;EACxC,IAAI;GAEF,MAAM,EAAE,cAAc,iFAAM;GAC5B,KAAK,SAAS,IAAI,UAAU,EAAE,UAAU,KAAK,OAAO,SAAS,CAAC;EAChE,SAAS,OAAO;GACd,QAAQ,MACN,wGACA,KACF;GACA,KAAK,UAAU;EACjB;CACF;CAEA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