{"version":3,"file":"useAnalytics.cjs","names":["useContext","IntlayerClientContext","useRef","isEnabled"],"sources":["../../../src/analytics/useAnalytics.tsx"],"sourcesContent":["import type { AnalyticsClient } from '@intlayer/analytics';\nimport { isEnabled } from '@intlayer/analytics/isEnabled';\nimport { useContext, useEffect, useRef } from 'preact/hooks';\nimport { IntlayerClientContext } from '../client/IntlayerProvider';\nimport { setExposureSink } from './exposureSink';\n\n/**\n * Initializes the Intlayer analytics client singleton when analytics is enabled\n * and keeps it aware of the current locale.\n *\n * Mirrors {@link useEditor}: the module is loaded via a dynamic `import()` so it\n * ships as its own async chunk, off the critical rendering path. Apps that\n * don't opt in (`analytics.enabled !== true`) pay nothing — the whole body is\n * guarded by `INTLAYER_ANALYTICS_ENABLED` and dead-code-eliminated at build\n * time.\n */\nexport const useAnalytics = () => {\n  const { locale } = useContext(IntlayerClientContext) ?? {};\n  const clientRef = useRef<AnalyticsClient | null>(null);\n\n  useEffect(() => {\n    if (process.env.INTLAYER_ANALYTICS_ENABLED === 'false' || !isEnabled) {\n      return;\n    }\n\n    let cancelled = false;\n    // Tracks whether initAnalyticsClient actually ran, so cleanup never calls\n    // stopAnalyticsClient for an init that was cancelled — that would decrement\n    // the shared reference count once too many.\n    let initialized = false;\n\n    import('@intlayer/analytics')\n      .then(({ initAnalyticsClient, buildContentExposure }) => {\n        if (cancelled) return;\n\n        const client = initAnalyticsClient();\n        initialized = true;\n        clientRef.current = client;\n\n        if (locale) client.setLocale(locale);\n        client.trackPageView({ reason: 'initial' });\n\n        // Route node-level exposures (from the interpreter plugins) into the\n        // client. Serialization happens here, off the hot render path.\n        setExposureSink((input) =>\n          client.trackContentExposure(buildContentExposure(input))\n        );\n      })\n      .catch(() => {\n        /* chunk failed to load — analytics stays a no-op */\n      });\n\n    return () => {\n      cancelled = true;\n      clientRef.current = null;\n      setExposureSink(null);\n      import('@intlayer/analytics')\n        .then(({ stopAnalyticsClient }) => {\n          if (initialized) stopAnalyticsClient();\n        })\n        .catch(() => {});\n    };\n    // Run once on mount; locale updates are handled by the effect below.\n  }, []);\n\n  useEffect(() => {\n    if (!locale || !clientRef.current) return;\n\n    clientRef.current.setLocale(locale);\n    clientRef.current.trackPageView({ reason: 'locale_change' });\n  }, [locale]);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AAgBA,MAAa,qBAAqB;CAChC,MAAM,EAAE,eAAWA,yBAAWC,qDAAqB,KAAK,CAAC;CACzD,MAAM,gBAAYC,qBAA+B,IAAI;CAErD,kCAAgB;EACd,IAAI,QAAQ,IAAI,+BAA+B,WAAW,CAACC,yCACzD;EAGF,IAAI,YAAY;EAIhB,IAAI,cAAc;EAElB,OAAO,sBAAsB,CAC1B,MAAM,EAAE,qBAAqB,2BAA2B;GACvD,IAAI,WAAW;GAEf,MAAM,SAAS,oBAAoB;GACnC,cAAc;GACd,UAAU,UAAU;GAEpB,IAAI,QAAQ,OAAO,UAAU,MAAM;GACnC,OAAO,cAAc,EAAE,QAAQ,UAAU,CAAC;GAI1C,gDAAiB,UACf,OAAO,qBAAqB,qBAAqB,KAAK,CAAC,CACzD;EACF,CAAC,CAAC,CACD,YAAY,CAEb,CAAC;EAEH,aAAa;GACX,YAAY;GACZ,UAAU,UAAU;GACpB,+CAAgB,IAAI;GACpB,OAAO,sBAAsB,CAC1B,MAAM,EAAE,0BAA0B;IACjC,IAAI,aAAa,oBAAoB;GACvC,CAAC,CAAC,CACD,YAAY,CAAC,CAAC;EACnB;CAEF,GAAG,CAAC,CAAC;CAEL,kCAAgB;EACd,IAAI,CAAC,UAAU,CAAC,UAAU,SAAS;EAEnC,UAAU,QAAQ,UAAU,MAAM;EAClC,UAAU,QAAQ,cAAc,EAAE,QAAQ,gBAAgB,CAAC;CAC7D,GAAG,CAAC,MAAM,CAAC;AACb"}