"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useTouch", {
    enumerable: true,
    get: function() {
        return useTouch;
    }
});
var _react = require("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 '';
}
function useTouch() {
    var startX = (0, _react.useRef)(0);
    var startY = (0, _react.useRef)(0);
    var deltaX = (0, _react.useRef)(0);
    var deltaY = (0, _react.useRef)(0);
    var delta = (0, _react.useRef)(0);
    var offsetX = (0, _react.useRef)(0);
    var offsetY = (0, _react.useRef)(0);
    var direction = (0, _react.useRef)('');
    var last = (0, _react.useRef)(false);
    var velocity = (0, _react.useRef)(0);
    var touchTime = (0, _react.useRef)(Date.now());
    var isVertical = function isVertical() {
        return direction.current === 'vertical';
    };
    var isHorizontal = function isHorizontal() {
        return direction.current === 'horizontal';
    };
    var reset = function reset() {
        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 getTouch(event) {
        var touch = event.touches ? event.touches[0] : event.nativeEvent;
        return touch;
    };
    var getX = function getX(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 getY(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 start(event) {
        reset();
        touchTime.current = Date.now();
        startX.current = getX(getTouch(event));
        startY.current = getY(getTouch(event));
    };
    var move = function move(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 end(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
    };
}
