"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useSheetSwipe", {
    enumerable: true,
    get: function() {
        return useSheetSwipe;
    }
});
var _lodashthrottle = /*#__PURE__*/ _interop_require_default(require("lodash.throttle"));
var _react = require("react");
function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
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 = _sliced_to_array((0, _react.useState)(true), 2), isTopScroll = _useState[0], setIsTopScroll = _useState[1];
    var isOverscroll = (0, _react.useRef)(false);
    var startY = (0, _react.useRef)(0);
    var currentY = (0, _react.useRef)(0);
    (0, _react.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 = (0, _lodashthrottle.default)(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
    ]);
};
