import { useCallback } from 'react';

import { useAnalytics } from '../analytics-context';

import type { EngagementEventData } from '../types';

type TrackEngagementFn = (
  data: Omit<EngagementEventData, 'userId'> & { userId?: string },
) => void;

export const useTrackEngagement = (): TrackEngagementFn => {
  const { trackEngagement } = useAnalytics();

  const track = useCallback(
    (data: Omit<EngagementEventData, 'userId'> & { userId?: string }) => {
      if (!data.userId) {
        console.warn('[Analytics] Engagement tracking requires userId');
        return;
      }

      trackEngagement(data as EngagementEventData);
    },
    [trackEngagement],
  );

  return track;
};
