import { useEffect, useRef } from 'react';

import { logEvent } from 'src/config/logging';
import { type EventName } from 'src/types/amplitude';

type Property = {
  usersCount: number;
};

type UseLogEventInViewVisitedProps = {
  properties?: Property;
  amplitudeEvent: EventName;
};

const useLogEventInViewVisited = (props: UseLogEventInViewVisitedProps) => {
  const { properties, amplitudeEvent } = props;
  const isViewVisited = useRef(false);

  useEffect(() => {
    if (isViewVisited.current) return;

    logEvent(amplitudeEvent, properties || undefined);

    isViewVisited.current = true;
  }, [properties, amplitudeEvent]);
};

export default useLogEventInViewVisited;
