import { useRef } from "react";
var MIN_DISTANCE = 10;
function getDirection(x, y) {
    if (x > y && x > MIN_DISTANCE) {
        return 'horizontal';
    }
    if (y > x && y > MIN_DISTANCE) {
        return 'vertical';
    }
    return '';
}
export function useTouch() {
    var startX = useRef(0);
    var startY = useRef(0);
    var deltaX = useRef(0);
    var deltaY = useRef(0);
    var delta = useRef(0);
    var offsetX = useRef(0);
    var offsetY = useRef(0);
    var direction = useRef('');
    var last = useRef(false);
    var velocity = useRef(0);
    var touchTime = useRef(Date.now());
    var isVertical = function() {
        return direction.current === 'vertical';
    };
    var isHorizontal = function() {
        return direction.current === 'horizontal';
    };
    var reset = function() {
        touchTime.current = Date.now();
        deltaX.current = 0;
        deltaY.current = 0;
        offsetX.current = 0;
        offsetY.current = 0;
        delta.current = 0;
        direction.current = '';
        last.current = false;
    };
    var getTouch = function(event) {
        var touch = event.touches ? event.touches[0] : event.nativeEvent;
        return touch;
    };
    var getX = function(touch) {
        if (typeof touch.clientX !== 'undefined' && typeof touch.pageX !== 'undefined') return touch.pageX;
        var _touch_screenX, _ref, _ref1;
        return (_ref1 = (_ref = (_touch_screenX = touch.screenX) !== null && _touch_screenX !== void 0 ? _touch_screenX : touch.pageX) !== null && _ref !== void 0 ? _ref : touch.clientX) !== null && _ref1 !== void 0 ? _ref1 : 0;
    };
    var getY = function(touch) {
        if (typeof touch.clientY !== 'undefined' && typeof touch.pageY !== 'undefined') return touch.pageY;
        var _touch_screenY, _ref, _ref1;
        return (_ref1 = (_ref = (_touch_screenY = touch.screenY) !== null && _touch_screenY !== void 0 ? _touch_screenY : touch.pageY) !== null && _ref !== void 0 ? _ref : touch.clientY) !== null && _ref1 !== void 0 ? _ref1 : 0;
    };
    var start = function(event) {
        reset();
        touchTime.current = Date.now();
        startX.current = getX(getTouch(event));
        startY.current = getY(getTouch(event));
    };
    var move = function(event) {
        var touch = getTouch(event);
        var clientX = getX(touch);
        var clientY = getY(touch);
        // Fix: Safari back will set clientX to negative number
        deltaX.current = clientX < 0 ? 0 : clientX - startX.current;
        deltaY.current = clientY - startY.current;
        offsetX.current = Math.abs(deltaX.current);
        offsetY.current = Math.abs(deltaY.current);
        delta.current = isVertical() ? deltaY.current : deltaX.current;
        if (!direction.current) {
            direction.current = getDirection(offsetX.current, offsetY.current);
        }
    };
    var end = function(event) {
        last.current = true;
        velocity.current = Math.sqrt(Math.pow(deltaX.current, 2) + Math.pow(deltaY.current, 2)) / (Date.now() - touchTime.current);
    };
    return {
        end: end,
        move: move,
        start: start,
        reset: reset,
        touchTime: touchTime,
        startX: startX,
        startY: startY,
        deltaX: deltaX,
        deltaY: deltaY,
        delta: delta,
        offsetX: offsetX,
        offsetY: offsetY,
        direction: direction,
        isVertical: isVertical,
        isHorizontal: isHorizontal,
        last: last
    };
}
