import { slicedToArray as _slicedToArray } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import throttle from 'lodash.throttle';
import { useState, useRef, useEffect } from 'react';

var SWIPE_THRESHOLD = 0.2;
var THROTTLE_DEFAULT_MS = 0;
var isScrollable = function isScrollable(element) {
  if (!element) {
    return false;
  }
  var style = getComputedStyle(element);
  return style.overflow === 'scroll' || style.overflow === 'auto' || style.overflowY === 'scroll' || style.overflowY === 'auto';
};
var useSheetSwipe = function useSheetSwipe(args) {
  var contentWrapperRef = args.contentWrapperRef,
    contentRef = args.contentRef,
    handleRef = args.handleRef,
    hasScrollEvents = args.hasScrollEvents,
    onClose = args.onClose,
    _args$throttleMs = args.throttleMs,
    throttleMs = _args$throttleMs === void 0 ? THROTTLE_DEFAULT_MS : _args$throttleMs;
  var _useState = useState(true),
    _useState2 = _slicedToArray(_useState, 2),
    isTopScroll = _useState2[0],
    setIsTopScroll = _useState2[1];
  var isOverscroll = useRef(false);
  var startY = useRef(0);
  var currentY = useRef(0);
  useEffect(function () {
    var contentWrapperEl = contentWrapperRef.current;
    var handleEl = handleRef.current;
    var contentEl = contentRef.current;
    var triggerElement = !isTopScroll ? handleEl : contentWrapperEl;
    if (!triggerElement || !contentEl || !contentWrapperEl || !hasScrollEvents) {
      return;
    }
    var nodes = Array.from(contentWrapperEl.querySelectorAll('*'));
    var scrollableElements = nodes.filter(isScrollable);
    contentWrapperEl.style.willChange = 'transform';
    var onTouchStart = function onTouchStart(event) {
      isOverscroll.current = false;
      startY.current = event.changedTouches[0].clientY;
      currentY.current = startY.current;
      contentWrapperEl.style.transition = 'none';
    };
    var onTouchMove = function onTouchMove(event) {
      var clientY = event.changedTouches[0].clientY;
      currentY.current = Math.max(startY.current, clientY);
      var offsetY = currentY.current - startY.current;
      if (isOverscroll.current) {
        startY.current = Infinity;
        return;
      }
      if (offsetY !== 0 && event.cancelable) {
        event.preventDefault();
      }
      contentWrapperEl.style.transform = "translateY(".concat(offsetY, "px)");
    };
    var onTouchEnd = function onTouchEnd(event) {
      isOverscroll.current = false;
      var curtainHeight = contentWrapperEl.offsetHeight;
      var endY = event.changedTouches[0].clientY;
      var offsetY = endY - startY.current;
      contentWrapperEl.style.transform = '';
      contentWrapperEl.style.transition = '';
      if (offsetY / curtainHeight > SWIPE_THRESHOLD) {
        onClose();
      }
    };
    var onScroll = throttle(function (event) {
      var onTop = event.target.scrollTop <= 0;
      if (!onTop) {
        isOverscroll.current = true;
      }
      setIsTopScroll(onTop);
    }, throttleMs);
    triggerElement.addEventListener('touchstart', onTouchStart);
    triggerElement.addEventListener('touchmove', onTouchMove);
    triggerElement.addEventListener('touchend', onTouchEnd);
    scrollableElements.forEach(function (element) {
      element.addEventListener('scroll', onScroll);
    });
    return function () {
      triggerElement.removeEventListener('touchstart', onTouchStart);
      triggerElement.removeEventListener('touchmove', onTouchMove);
      triggerElement.removeEventListener('touchend', onTouchEnd);
      scrollableElements.forEach(function (element) {
        element.removeEventListener('scroll', onScroll);
      });
    };
  }, [isTopScroll]);
};

export { useSheetSwipe };
