"use strict";
const React = require("react");
const dom = require("../../_util/dom.js");
function useHeaderScroll(props) {
  var headerWrapperRef = props.headerWrapperRef, headerOffset = props.headerOffset, align = props.align, isScrollable = props.isScrollable, direction = props.direction, onScroll = props.onScroll;
  function onOffset(offsetX, offsetY) {
    var offset = 0;
    if (direction === "vertical") {
      offset = headerOffset + offsetY;
    } else {
      offset = align === "left" ? headerOffset + offsetX : headerOffset - offsetX;
    }
    onScroll && onScroll(offset);
  }
  var lastWheelDirectionRef = React.useRef("x");
  function onWheel(e) {
    if (!isScrollable)
      return;
    e.preventDefault();
    var deltaX = e.deltaX, deltaY = e.deltaY;
    var offset = 0;
    var absX = Math.abs(deltaX);
    var absY = Math.abs(deltaY);
    if (absX === absY) {
      offset = lastWheelDirectionRef.current === "x" ? deltaX : deltaY;
    } else if (absX > absY) {
      offset = deltaX;
      lastWheelDirectionRef.current = "x";
    } else {
      offset = deltaY;
      lastWheelDirectionRef.current = "y";
    }
    onOffset(offset, offset);
  }
  var positionRef = React.useRef({
    clientX: 0,
    clientY: 0
  });
  var getPosition = function(e) {
    return e && e.touches && e.touches.length && e.touches[0];
  };
  var onTouchMove = function(e) {
    if (e.cancelable)
      e.preventDefault();
    var position = getPosition(e);
    if (!position)
      return;
    var _a = positionRef.current, clientX = _a.clientX, clientY = _a.clientY;
    var offsetX = position.clientX - clientX;
    var offsetY = position.clientY - clientY;
    onOffset(-offsetX, -offsetY);
  };
  var onTouchMoveEnd = function() {
    dom.off(document.documentElement, "touchmove", onTouchMove);
    dom.off(document.documentElement, "touchend", onTouchMoveEnd);
  };
  var onTouchStart = function(e) {
    if (!isScrollable)
      return;
    var position = getPosition(e);
    if (!position)
      return;
    positionRef.current = {
      clientX: position.clientX,
      clientY: position.clientY
    };
    dom.on(document.documentElement, "touchmove", onTouchMove, { passive: false });
    dom.on(window, "touchend", onTouchMoveEnd, { passive: false });
  };
  var eventProxy = React.useRef(null);
  eventProxy.current = { onWheel, onTouchStart };
  React.useEffect(function() {
    dom.on(headerWrapperRef.current, "wheel", function(e) {
      eventProxy.current.onWheel(e);
    }, { passive: false });
    dom.on(headerWrapperRef.current, "touchstart", function(e) {
      eventProxy.current.onTouchStart(e);
    }, { passive: true });
  }, [headerWrapperRef.current]);
}
module.exports = useHeaderScroll;
//# sourceMappingURL=useHeaderScroll.js.map
