{"version":3,"file":"posthog-adapter.cjs","names":[],"sources":["../../src/telemetry/posthog-adapter.ts"],"sourcesContent":["import type { TelemetryAdapter, TelemetryEvent, TelemetryUser } from \"./types\";\n\n/**\n * Minimal subset of `posthog-js` used by the adapter. Pass either the real\n * default export (`import posthog from \"posthog-js\"`) or a stubbed object\n * in tests.\n */\nexport interface PostHogLike {\n    init?: (apiKey: string, options?: Record<string, unknown>) => void;\n    identify: (distinctId: string, properties?: Record<string, unknown>) => void;\n    capture: (eventName: string, properties?: Record<string, unknown>) => void;\n    captureException?: (error: unknown, properties?: Record<string, unknown>) => void;\n    reset?: () => void;\n}\n\nexport interface CreatePostHogTelemetryAdapterOptions {\n    /** The PostHog JS client (the default export of `posthog-js`). Required. */\n    posthog: PostHogLike;\n    /** Project API key + options. When provided, `Provider.init` calls `posthog.init(apiKey, options)`. */\n    init?: { apiKey: string; options?: Record<string, unknown> };\n}\n\nfunction userToProperties(user: TelemetryUser): Record<string, unknown> {\n    const { email, name, traits } = user;\n    return {\n        ...(email !== undefined ? { email } : null),\n        ...(name !== undefined ? { name } : null),\n        ...(traits ?? null),\n    };\n}\n\n/**\n * Build a [[TelemetryAdapter]] backed by [`posthog-js`](https://posthog.com/docs/libraries/js).\n * The PostHog client is supplied by the caller (not bundled).\n *\n * Mapping:\n * - `identify(user)` → `posthog.identify(user.id, { email, name, ...traits })` (or `posthog.reset()` when `null`)\n * - `track({ name, properties })` → `posthog.capture(name, properties)`\n * - `captureException(err, ctx)` → `posthog.captureException(err, ctx)` when available, otherwise `posthog.capture(\"$exception\", { …err, …ctx })`\n * - `flush()` → no-op (PostHog batches automatically)\n *\n * @example\n * import posthog from \"posthog-js\";\n * import { createPostHogTelemetryAdapter, TelemetryProvider } from \"tempest-react-sdk\";\n *\n * const adapter = createPostHogTelemetryAdapter({\n *     posthog,\n *     init: { apiKey: import.meta.env.VITE_POSTHOG_KEY, options: { api_host: \"https://us.i.posthog.com\" } },\n * });\n *\n * <TelemetryProvider adapter={adapter}><App /></TelemetryProvider>;\n */\nexport function createPostHogTelemetryAdapter(\n    options: CreatePostHogTelemetryAdapterOptions,\n): TelemetryAdapter {\n    const { posthog, init } = options;\n\n    return {\n        init() {\n            if (init && posthog.init) {\n                posthog.init(init.apiKey, init.options);\n            }\n        },\n        identify(user: TelemetryUser | null) {\n            if (user === null) {\n                posthog.reset?.();\n                return;\n            }\n            if (user.id === undefined) return;\n            posthog.identify(user.id, userToProperties(user));\n        },\n        track(event: TelemetryEvent) {\n            posthog.capture(event.name, event.properties);\n        },\n        captureException(error: unknown, context?: Record<string, unknown>) {\n            if (posthog.captureException) {\n                posthog.captureException(error, context);\n                return;\n            }\n            const err = error instanceof Error ? error : new Error(String(error));\n            posthog.capture(\"$exception\", {\n                $exception_message: err.message,\n                $exception_type: err.name,\n                $exception_stack_trace_raw: err.stack,\n                ...context,\n            });\n        },\n    };\n}\n"],"mappings":"AAsBA,SAAS,EAAiB,EAA8C,CACpE,GAAM,CAAE,QAAO,OAAM,UAAW,EAChC,MAAO,CACH,GAAI,IAAU,IAAA,GAAwB,KAAZ,CAAE,OAAM,EAClC,GAAI,IAAS,IAAA,GAAuB,KAAX,CAAE,MAAK,EAChC,GAAI,GAAU,IAClB,CACJ,CAuBA,SAAgB,EACZ,EACgB,CAChB,GAAM,CAAE,UAAS,QAAS,EAE1B,MAAO,CACH,MAAO,CACC,GAAQ,EAAQ,MAChB,EAAQ,KAAK,EAAK,OAAQ,EAAK,OAAO,CAE9C,EACA,SAAS,EAA4B,CACjC,GAAI,IAAS,KAAM,CACf,EAAQ,QAAQ,EAChB,MACJ,CACI,EAAK,KAAO,IAAA,IAChB,EAAQ,SAAS,EAAK,GAAI,EAAiB,CAAI,CAAC,CACpD,EACA,MAAM,EAAuB,CACzB,EAAQ,QAAQ,EAAM,KAAM,EAAM,UAAU,CAChD,EACA,iBAAiB,EAAgB,EAAmC,CAChE,GAAI,EAAQ,iBAAkB,CAC1B,EAAQ,iBAAiB,EAAO,CAAO,EACvC,MACJ,CACA,IAAM,EAAM,aAAiB,MAAQ,EAAY,MAAM,OAAO,CAAK,CAAC,EACpE,EAAQ,QAAQ,aAAc,CAC1B,mBAAoB,EAAI,QACxB,gBAAiB,EAAI,KACrB,2BAA4B,EAAI,MAChC,GAAG,CACP,CAAC,CACL,CACJ,CACJ"}