import debounce from 'lodash/debounce';

export default function useVerticalScroll(tableWrapper: HTMLElement) {
  // handsontable 主体部分元素
  let $holder: HTMLElement | null;

  // 滚动监听器防抖
  const debouncedScroll = debounce(() => {
    if ($holder) {
      if ($holder.scrollTop - 1 >= 0) {
        $holder.scrollTop -= 1;
      }

      $holder.removeEventListener('scroll', debouncedScroll);
    }
  }, 100);

  const registerVerticalScroll = () => {
    // handsontable 主体部分的内容的 holder
    $holder = tableWrapper.querySelector('.ht_master .wtHolder');
    $holder && $holder.addEventListener('scroll', debouncedScroll);
  };

  return {
    registerVerticalScroll,
  };
}
