import throttle from 'lodash/throttle';
import { useEffect } from 'react';
import Events from '../utils/events';

var useScroll = function useScroll(_ref) {
  var container = _ref.container,
      onScroll = _ref.onScroll,
      _ref$wait = _ref.wait,
      wait = _ref$wait === void 0 ? 200 : _ref$wait;
  useEffect(function () {
    var handler = throttle(function (event) {
      // console.log(`[${Date.now()}] handler`);
      typeof onScroll === 'function' && onScroll(event);
    }, wait);

    if (container.current) {
      Events.on(container.current, 'scroll', handler);
    }

    return function () {
      if (container.current) {
        Events.off(container.current, 'scroll', handler);
      }
    };
  }, [container]);
};

export default useScroll;