'use strict';

var React = require('react');
var consts = require('./consts-a5382bc1.js');

function calcScrollBottom(scrollHeight, scrollTop) {
  return scrollHeight - scrollTop;
}
function useHandleOnScrollCallback(_ref) {
  let {
    hasMore,
    onScroll,
    scrollRef,
    setShowScrollDownButton
  } = _ref;
  return React.useCallback(() => {
    const element = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current;
    if (element == null) {
      return;
    }
    const {
      scrollTop,
      scrollHeight,
      clientHeight
    } = element;
    // https://sendbird.atlassian.net/browse/SBISSUE-11759
    // the edge case where channel is inside a page that already has scroll
    // scrollintoView will move the whole page, which we dont want
    const scrollBottom = calcScrollBottom(scrollHeight, scrollTop);
    // even if there is more to fetch or not,
    // we still have to show the scroll to bottom button
    if (typeof setShowScrollDownButton === 'function') {
      setShowScrollDownButton(scrollHeight > scrollTop + clientHeight + 1);
    }
    if (!hasMore) {
      return;
    }
    if (scrollTop < consts.SCROLL_BUFFER) {
      onScroll(() => {
        // sets the scroll position to the bottom of the new messages
        element.scrollTop = element.scrollHeight - scrollBottom;
      });
    }
  }, [setShowScrollDownButton, hasMore, onScroll, scrollRef]);
}

exports.useHandleOnScrollCallback = useHandleOnScrollCallback;
//# sourceMappingURL=index-8049c2a3.js.map
