"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = require("react");
var _getAncestors = _interopRequireDefault(require("../mixins/getAncestors"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var _default = exports["default"] = function _default(target, callback) {
  return (0, _react.useEffect)(function () {
    var removeScrollListeners;
    var addScrollListeners = function addScrollListeners() {
      var scrollAncestors = (0, _getAncestors["default"])(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);
    return function () {
      removeScrollListeners();
      window.removeEventListener("resize", onResize);
    };
  }, [target, callback]);
};