{"version":3,"file":"PostHogProvider.cjs","sources":["../../src/provider/PostHogProvider.tsx"],"sourcesContent":["import { useEffect, type ReactNode } from 'react';\nimport { dropBenignBrowserExceptions } from '../lib/posthog/before-send';\nimport { maskSensitiveInput } from '../lib/posthog/mask-sensitive-input';\nimport { wrapPosthogClient } from '../lib/posthog/wrap-posthog-client';\n\n// ---------------------------------------------------------------------------\n//  PostHog configuration\n// ---------------------------------------------------------------------------\n\nexport interface PostHogConfig {\n  /** PostHog API key. When falsy, PostHog is disabled and the provider is a no-op. */\n  apiKey?: string;\n  /** PostHog ingestion host. Defaults to 'https://us.i.posthog.com'. */\n  host?: string;\n  /** Enable debug mode (logs PostHog internals to the console). */\n  debug?: boolean;\n}\n\n// ---------------------------------------------------------------------------\n//  Lazy singleton — avoids importing posthog-js when it's not installed or\n//  not configured.  All access goes through the helpers below.\n// ---------------------------------------------------------------------------\n\n/** The resolved PostHog instance (or null when disabled / not yet loaded). */\nlet posthogInstance: import('posthog-js').PostHog | null = null;\nlet initPromise: Promise<import('posthog-js').PostHog | null> | null = null;\n\nconst DEFAULT_HOST = 'https://us.i.posthog.com';\n\nasync function initializePostHog(\n  apiKey: string,\n  host: string,\n  debug: boolean,\n): Promise<import('posthog-js').PostHog | null> {\n  if (posthogInstance) return posthogInstance;\n  if (typeof window === 'undefined') return null;\n\n  try {\n    // Dynamic import — tree-shaken away when posthog-js is not installed.\n    const posthog = (await import('posthog-js')).default;\n\n    posthog.init(apiKey, {\n      api_host: host,\n      autocapture: true,\n      before_send: dropBenignBrowserExceptions,\n      capture_pageview: false,\n      session_recording: {\n        recordCrossOriginIframes: true,\n        maskAllInputs: true,\n        maskInputFn: maskSensitiveInput,\n      },\n      loaded: (ph) => {\n        wrapPosthogClient(ph as import('posthog-js').PostHog);\n        if (debug) {\n          ph.debug(true);\n        }\n      },\n    });\n\n    wrapPosthogClient(posthog);\n\n    posthogInstance = posthog;\n    return posthog;\n  } catch {\n    // posthog-js is not installed or failed to load — silently degrade.\n    // Reset initPromise so future renders can retry if the failure was transient.\n    initPromise = null;\n    return null;\n  }\n}\n\n// ---------------------------------------------------------------------------\n//  Public helper — returns the current PostHog instance (if any).\n// ---------------------------------------------------------------------------\n\nexport function getPostHogInstance<TClient = unknown>(): TClient | null {\n  return posthogInstance as TClient | null;\n}\n\n// ---------------------------------------------------------------------------\n//  React provider\n// ---------------------------------------------------------------------------\n\nexport function PostHogProvider({\n  children,\n  config,\n}: {\n  children: ReactNode;\n  config?: PostHogConfig;\n}) {\n  const apiKey: string = config?.apiKey ?? '';\n  const host: string = config?.host ?? DEFAULT_HOST;\n  const debug: boolean = config?.debug ?? false;\n\n  useEffect(() => {\n    if (!apiKey || typeof window === 'undefined') return;\n\n    if (!initPromise) {\n      initPromise = initializePostHog(apiKey, host, debug);\n    }\n  }, [apiKey, host, debug]);\n\n  // Always render children — PostHog is purely additive.\n  return <>{children}</>;\n}\n"],"names":["dropBenignBrowserExceptions","maskSensitiveInput","wrapPosthogClient","useEffect"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwBA,IAAI,kBAAuD;AAC3D,IAAI,cAAmE;AAEvE,MAAM,eAAe;AAErB,eAAe,kBACb,QACA,MACA,OAC8C;AAC9C,MAAI,gBAAiB,QAAO;AAC5B,MAAI,OAAO,WAAW,YAAa,QAAO;AAE1C,MAAI;AAEF,UAAM,WAAW,MAAM,OAAO,YAAY,GAAG;AAE7C,YAAQ,KAAK,QAAQ;AAAA,MACnB,UAAU;AAAA,MACV,aAAa;AAAA,MACb,aAAaA,WAAAA;AAAAA,MACb,kBAAkB;AAAA,MAClB,mBAAmB;AAAA,QACjB,0BAA0B;AAAA,QAC1B,eAAe;AAAA,QACf,aAAaC,mBAAAA;AAAAA,MAAA;AAAA,MAEf,QAAQ,CAAC,OAAO;AACdC,0BAAAA,kBAAkB,EAAkC;AACpD,YAAI,OAAO;AACT,aAAG,MAAM,IAAI;AAAA,QACf;AAAA,MACF;AAAA,IAAA,CACD;AAEDA,sBAAAA,kBAAkB,OAAO;AAEzB,sBAAkB;AAClB,WAAO;AAAA,EACT,QAAQ;AAGN,kBAAc;AACd,WAAO;AAAA,EACT;AACF;AAMO,SAAS,qBAAwD;AACtE,SAAO;AACT;AAMO,SAAS,gBAAgB;AAAA,EAC9B;AAAA,EACA;AACF,GAGG;AACD,QAAM,UAAiB,iCAAQ,WAAU;AACzC,QAAM,QAAe,iCAAQ,SAAQ;AACrC,QAAM,SAAiB,iCAAQ,UAAS;AAExCC,aAAAA,UAAU,MAAM;AACd,QAAI,CAAC,UAAU,OAAO,WAAW,YAAa;AAE9C,QAAI,CAAC,aAAa;AAChB,oBAAc,kBAAkB,QAAQ,MAAM,KAAK;AAAA,IACrD;AAAA,EACF,GAAG,CAAC,QAAQ,MAAM,KAAK,CAAC;AAGxB,+DAAU,UAAS;AACrB;;;"}