"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.addPointerEventListeners = addPointerEventListeners;
exports.getEventCoordinates = getEventCoordinates;
exports.getEventTarget = getEventTarget;
exports.hasMovedEnough = hasMovedEnough;
exports.isTouchEvent = isTouchEvent;
exports.removePointerEventListeners = removePointerEventListeners;
// 统一获取事件坐标的函数
function getEventCoordinates(event) {
  if ('touches' in event && event.touches.length > 0) {
    return {
      clientX: event.touches[0].clientX,
      clientY: event.touches[0].clientY
    };
  } else if ('changedTouches' in event && event.changedTouches.length > 0) {
    return {
      clientX: event.changedTouches[0].clientX,
      clientY: event.changedTouches[0].clientY
    };
  } else {
    return {
      clientX: event.clientX,
      clientY: event.clientY
    };
  }
}
// 获取事件目标元素
function getEventTarget(event) {
  if ('touches' in event && event.touches.length > 0) {
    return document.elementFromPoint(event.touches[0].clientX, event.touches[0].clientY);
  }
  // 处理touchend事件，此时touches为空，需要使用changedTouches
  if ('changedTouches' in event && event.changedTouches.length > 0) {
    return document.elementFromPoint(event.changedTouches[0].clientX, event.changedTouches[0].clientY);
  }
  return event.target;
}
// 判断是否为触摸事件
function isTouchEvent(event) {
  return 'touches' in event;
}
// 为元素添加统一的指针事件监听器
function addPointerEventListeners(element, handlers, isTouchStart) {
  if (isTouchStart) {
    element.addEventListener('touchmove', handlers.onPointerMove, {
      passive: false
    });
    element.addEventListener('touchend', handlers.onPointerUp, {
      passive: false
    });
  } else {
    element.addEventListener('mousemove', handlers.onPointerMove);
    element.addEventListener('mouseup', handlers.onPointerUp);
  }
}
// 移除统一的指针事件监听器
function removePointerEventListeners(element, handlers, isTouchStart) {
  if (isTouchStart) {
    element.removeEventListener('touchmove', handlers.onPointerMove);
    element.removeEventListener('touchend', handlers.onPointerUp);
  } else {
    element.removeEventListener('mousemove', handlers.onPointerMove);
    element.removeEventListener('mouseup', handlers.onPointerUp);
  }
}
// 检查是否移动了足够的距离（用于区分点击和拖拽）
function hasMovedEnough(startX, startY, endX, endY) {
  var threshold = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 5;
  var xDiff = endX - startX;
  var yDiff = endY - startY;
  return Math.sqrt(xDiff * xDiff + yDiff * yDiff) > threshold;
}