import { useEffect } from "react";
import getAncestors from "../mixins/getAncestors";
export default (function (target, callback, dropRef) {
  return useEffect(function () {
    var removeScrollListeners;
    var addScrollListeners = function addScrollListeners() {
      var scrollAncestors = getAncestors(target).filter(function (node) {
        return node.scrollHeight > node.clientHeight;
      });
      scrollAncestors.forEach(function (node) {
        return node.addEventListener("scroll", callback, {
          capture: false,
          passive: true
        });
      });
      return function () {
        return scrollAncestors.forEach(function (node) {
          return node.removeEventListener("scroll", callback);
        });
      };
    };
    removeScrollListeners = addScrollListeners();
    var onResize = function onResize() {
      removeScrollListeners();
      removeScrollListeners = addScrollListeners();
      callback();
    };
    window.addEventListener("resize", onResize);
    var resizeObserver;
    if (dropRef != null && dropRef.current) {
      resizeObserver = new ResizeObserver(callback);
      resizeObserver.observe(dropRef.current);
    }
    return function () {
      removeScrollListeners();
      window.removeEventListener("resize", onResize);
      if (resizeObserver) resizeObserver.disconnect();
    };
  }, [target, callback, dropRef]);
});