/**
 * Umami analytics wrapper — safe server-side, no-op when not configured.
 * Usage: const { track } = useTracking()
 *        track('player:open')
 *        track('axis:navigate', { axis: 'numerique' })
 */
export function useTracking() {
  const runtimeConfig = useRuntimeConfig()
  const enabled = !!runtimeConfig.public.umamiWebsiteId

  function track(event: string, data?: Record<string, unknown>) {
    if (!import.meta.client || !enabled) return
    const umami = (window as Record<string, unknown>).umami as
      | { track: (event: string, data?: unknown) => void }
      | undefined
    umami?.track(event, data)
  }

  return { track, enabled }
}
