"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useTouch = void 0;

var _react = require("react");

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { 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 _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(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(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(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 _iterableToArrayLimit(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 _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var getPostion = function getPostion(e) {
  // @ts-ignore
  if (e.pageX) {
    // @ts-ignore
    var pageX = e.pageX,
        pageY = e.pageY;
    return {
      x: pageX,
      y: pageY
    };
  } // @ts-ignore


  if (e.nativeEvent.pageX) {
    // @ts-ignore
    var _e$nativeEvent = e.nativeEvent,
        _pageX = _e$nativeEvent.pageX,
        _pageY = _e$nativeEvent.pageY;
    return {
      x: _pageX,
      y: _pageY
    };
  } // @ts-ignore


  if (e.nativeEvent.changedTouches) {
    // @ts-ignore
    var _e$nativeEvent$change = e.nativeEvent.changedTouches[0],
        _pageX2 = _e$nativeEvent$change.pageX,
        _pageY2 = _e$nativeEvent$change.pageY;
    return {
      x: _pageX2,
      y: _pageY2
    };
  }

  return {
    x: 0,
    y: 0
  };
};

var useTouch = function useTouch(callback) {
  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      down = _useState2[0],
      setDown = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      _useState4 = _slicedToArray(_useState3, 2),
      isCancel = _useState4[0],
      setIsCancel = _useState4[1];

  var _useState5 = (0, _react.useState)([0, 0]),
      _useState6 = _slicedToArray(_useState5, 2),
      lastOffset = _useState6[0],
      setLastOffset = _useState6[1];

  var _useState7 = (0, _react.useState)({
    x: 0,
    y: 0
  }),
      _useState8 = _slicedToArray(_useState7, 2),
      startPostion = _useState8[0],
      setStartPostion = _useState8[1];

  var cancel = function cancel() {
    setDown(false);
    setIsCancel(true);
  };

  var onTouchMove = function onTouchMove(e) {
    if (!down || isCancel) return;

    var _getPostion = getPostion(e),
        x = _getPostion.x,
        y = _getPostion.y;

    callback({
      event: e,
      down: true,
      movement: [x - startPostion.x, y - startPostion.y],
      cancel: cancel,
      lastOffset: lastOffset
    });
  };

  var onTouchStart = function onTouchStart(e) {
    var _getPostion2 = getPostion(e),
        x = _getPostion2.x,
        y = _getPostion2.y;

    setStartPostion({
      x: x,
      y: y
    });
    setDown(true);
    callback({
      event: e,
      down: true,
      movement: [0, 0],
      cancel: cancel,
      lastOffset: lastOffset
    });
  };

  var onTouchEnd = function onTouchEnd(e) {
    var _getPostion3 = getPostion(e),
        x = _getPostion3.x,
        y = _getPostion3.y;

    setLastOffset([lastOffset[0] + x - startPostion.x, lastOffset[1] + y - startPostion.y]);
    if (down) callback({
      event: e,
      down: false,
      movement: [x - startPostion.x, y - startPostion.y],
      cancel: cancel,
      lastOffset: lastOffset
    });
    setDown(false);
    setIsCancel(false);
  };

  (0, _react.useEffect)(function () {
    document.body.addEventListener('mousemove', onTouchMove);
    document.body.addEventListener('touchmove', onTouchMove);
    document.body.addEventListener('mouseup', onTouchEnd);
    document.body.addEventListener('touchend', onTouchEnd);
    return function () {
      document.body.removeEventListener('mousemove', onTouchMove);
      document.body.removeEventListener('touchmove', onTouchMove);
      document.body.removeEventListener('mouseup', onTouchEnd);
      document.body.removeEventListener('touchend', onTouchEnd);
    };
  }, [down, isCancel, lastOffset, startPostion]);
  return {
    onTouchStart: onTouchStart,
    onMouseDown: onTouchStart // onTouchMove,
    // onMouseMove: onTouchMove,
    // onTouchEnd,
    // onMouseUp: onTouchEnd,

  };
};

exports.useTouch = useTouch;