type OnVisibleOnceOptions = IntersectionObserverInit;

export const onVisibleOnce = (
  target: Element | null,
  callback: () => void | Promise<void>,
  options: OnVisibleOnceOptions = {
    threshold: 0,
    rootMargin: "200px",
  },
) => {
  if (!target) return;

  const observer = new IntersectionObserver(([entry]) => {
    if (!entry.isIntersecting) return;

    observer.unobserve(target);
    callback();
  }, options);

  observer.observe(target);
};
