"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const isNodeOrChild = require("./utils/is-node-or-child.js");
const usePointerEvent = require("../events/use-pointer-event.js");
const useUnmountEffect = require("../utils/use-unmount-effect.js");
const types = require("../render/utils/types.js");
const lock = require("./drag/utils/lock.js");
const pipe = require("../../../../../../popmotion@11.0.3/node_modules/popmotion/dist/es/utils/pipe.js");
function useTapGesture(_a) {
  var onTap = _a.onTap, onTapStart = _a.onTapStart, onTapCancel = _a.onTapCancel, whileTap = _a.whileTap, visualElement = _a.visualElement;
  var hasPressListeners = onTap || onTapStart || onTapCancel || whileTap;
  var isPressing = React.useRef(false);
  var cancelPointerEndListeners = React.useRef(null);
  var eventOptions = {
    passive: !(onTapStart || onTap || onTapCancel || onPointerDown)
  };
  function removePointerEndListener() {
    var _a2;
    (_a2 = cancelPointerEndListeners.current) === null || _a2 === void 0 ? void 0 : _a2.call(cancelPointerEndListeners);
    cancelPointerEndListeners.current = null;
  }
  function checkPointerEnd() {
    var _a2;
    removePointerEndListener();
    isPressing.current = false;
    (_a2 = visualElement.animationState) === null || _a2 === void 0 ? void 0 : _a2.setActive(types.AnimationType.Tap, false);
    return !lock.isDragActive();
  }
  function onPointerUp(event, info) {
    if (!checkPointerEnd())
      return;
    !isNodeOrChild.isNodeOrChild(visualElement.getInstance(), event.target) ? onTapCancel === null || onTapCancel === void 0 ? void 0 : onTapCancel(event, info) : onTap === null || onTap === void 0 ? void 0 : onTap(event, info);
  }
  function onPointerCancel(event, info) {
    if (!checkPointerEnd())
      return;
    onTapCancel === null || onTapCancel === void 0 ? void 0 : onTapCancel(event, info);
  }
  function onPointerDown(event, info) {
    var _a2;
    removePointerEndListener();
    if (isPressing.current)
      return;
    isPressing.current = true;
    cancelPointerEndListeners.current = pipe.pipe(usePointerEvent.addPointerEvent(window, "pointerup", onPointerUp, eventOptions), usePointerEvent.addPointerEvent(window, "pointercancel", onPointerCancel, eventOptions));
    (_a2 = visualElement.animationState) === null || _a2 === void 0 ? void 0 : _a2.setActive(types.AnimationType.Tap, true);
    onTapStart === null || onTapStart === void 0 ? void 0 : onTapStart(event, info);
  }
  usePointerEvent.usePointerEvent(visualElement, "pointerdown", hasPressListeners ? onPointerDown : void 0, eventOptions);
  useUnmountEffect.useUnmountEffect(removePointerEndListener);
}
exports.useTapGesture = useTapGesture;
//# sourceMappingURL=use-tap-gesture.js.map
