import { useEffect, useRef } from 'react';

export const useScrollTarget = () => {
  const scrollTarget = useRef<HTMLElement | null>(null);

  useEffect(() => {
    const rootEl = document.getElementById('root') as HTMLElement;
    const pullToRefreshEl = document.getElementsByClassName('ptr__children')[0] as HTMLElement;

    let target: HTMLElement | null = rootEl;
    if (pullToRefreshEl?.scrollHeight > window.innerHeight) {
      target = pullToRefreshEl;
    }
    scrollTarget.current = target;

    return () => {
      scrollTarget.current = null;
    };
  }, []);

  return scrollTarget;
};
