{"version":3,"file":"useAnalytics.cjs","names":["isEnabled","inject","DestroyRef","Injector","INTLAYER_TOKEN","signal","runInInjectionContext","effect","provideAppInitializer"],"sources":["../../../src/analytics/useAnalytics.ts"],"sourcesContent":["import {\n  DestroyRef,\n  effect,\n  Injector,\n  inject,\n  provideAppInitializer,\n  runInInjectionContext,\n  signal,\n} from '@angular/core';\nimport type { AnalyticsClient } from '@intlayer/analytics';\nimport { isEnabled } from '@intlayer/analytics/isEnabled';\n// Import from the standalone token file to avoid a circular dependency:\n//   installIntlayer.ts → useAnalytics.ts → ../client → installIntlayer.ts\nimport { INTLAYER_TOKEN, type IntlayerProvider } from '../client/intlayerToken';\nimport { setExposureSink } from './exposureSink';\n\n/**\n * Initializes the Intlayer analytics client singleton when analytics is\n * enabled (mirrors {@link useEditor}). Records an initial page view, keeps the\n * client aware of the current locale, and wires node-level content exposures.\n *\n * Must be called inside an Angular injection context (e.g. a component\n * constructor, `provideAppInitializer`, or `runInInjectionContext`).\n *\n * @param client - The IntlayerProvider instance to sync locale from.\n *   When omitted the function injects `INTLAYER_TOKEN` from the DI tree,\n *   so it still works when called directly from a component.\n */\nexport const useAnalytics = (client?: IntlayerProvider | null): void => {\n  if (process.env.INTLAYER_ANALYTICS_ENABLED === 'false' || !isEnabled) return;\n\n  const destroyRef = inject(DestroyRef, { optional: true });\n  const injector = inject(Injector);\n\n  // Resolve the client: use the passed-in reference or fall back to injection.\n  const resolvedClient =\n    client ??\n    inject<IntlayerProvider>(INTLAYER_TOKEN, { optional: true } as any);\n\n  // `analyticsClient` signal is set once the async import resolves.\n  // Using a signal lets an `effect()` react to it becoming available.\n  const analyticsClient = signal<AnalyticsClient | null>(null);\n\n  // Guard: prevents the async callback from acting after the view is destroyed.\n  let stopped = false;\n  // Tracks whether initAnalyticsClient actually ran, so teardown 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 (stopped) return;\n\n      const clientInstance = initAnalyticsClient();\n      initialized = true;\n\n      const currentLocale = resolvedClient?.locale();\n      if (currentLocale) clientInstance.setLocale(currentLocale);\n      clientInstance.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        clientInstance.trackContentExposure(buildContentExposure(input))\n      );\n\n      analyticsClient.set(clientInstance);\n    })\n    .catch(() => {\n      /* chunk failed to load — analytics stays a no-op */\n    });\n\n  // Locale switches are a real signal — record them as page views.\n  let previousLocale: string | undefined;\n  const effectRef = runInInjectionContext(injector, () =>\n    effect(() => {\n      const clientInstance = analyticsClient();\n      const locale = resolvedClient?.locale();\n\n      if (!clientInstance || !locale) return;\n\n      if (previousLocale === undefined) {\n        // First run after init — the initial page view already covered it.\n        previousLocale = locale;\n        return;\n      }\n      if (previousLocale === locale) return;\n\n      previousLocale = locale;\n      clientInstance.setLocale(locale);\n      clientInstance.trackPageView({ reason: 'locale_change' });\n    })\n  );\n\n  // Tear down on destroy\n  destroyRef?.onDestroy(() => {\n    stopped = true;\n    effectRef.destroy();\n    analyticsClient.set(null);\n    setExposureSink(null);\n    import('@intlayer/analytics')\n      .then(({ stopAnalyticsClient }) => {\n        if (initialized) stopAnalyticsClient();\n      })\n      .catch(() => {});\n  });\n};\n\n/**\n * Angular provider that wires `useAnalytics` into the application\n * initialisation phase via `provideAppInitializer`.\n *\n * `provideIntlayer()` already calls this internally, so you only need this\n * function when you want to manage providers individually.\n *\n * @example\n * ```ts\n * // app.config.ts\n * import { provideIntlayer, provideIntlayerAnalytics } from 'angular-intlayer';\n *\n * export const appConfig: ApplicationConfig = {\n *   providers: [provideIntlayer(), provideIntlayerAnalytics()],\n * };\n * ```\n */\nexport const provideIntlayerAnalytics = (client?: IntlayerProvider | null) =>\n  provideAppInitializer(() => useAnalytics(client));\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA4BA,MAAa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