"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const isActive = require("./dist-es-gestures-drag-state-is-active.cjs");
const isNodeOrChild = require("./dist-es-gestures-utils-is-node-or-child.cjs");
const isPrimaryPointer = require("./dist-es-gestures-utils-is-primary-pointer.cjs");
const setup = require("./dist-es-gestures-utils-setup.cjs");
const isKeyboardAccessible = require("./dist-es-gestures-press-utils-is-keyboard-accessible.cjs");
const keyboard = require("./dist-es-gestures-press-utils-keyboard.cjs");
const state = require("./dist-es-gestures-press-utils-state.cjs");
function isValidPressEvent(event) {
  return isPrimaryPointer.isPrimaryPointer(event) && !isActive.isDragActive();
}
function press(elementOrSelector, onPressStart, options = {}) {
  const [elements, eventOptions, cancelEvents] = setup.setupGesture(elementOrSelector, options);
  const startPress = (startEvent) => {
    const element = startEvent.currentTarget;
    if (!isValidPressEvent(startEvent) || state.isPressing.has(element))
      return;
    state.isPressing.add(element);
    const onPressEnd = onPressStart(startEvent);
    const onPointerEnd = (endEvent, success) => {
      window.removeEventListener("pointerup", onPointerUp);
      window.removeEventListener("pointercancel", onPointerCancel);
      if (!isValidPressEvent(endEvent) || !state.isPressing.has(element)) {
        return;
      }
      state.isPressing.delete(element);
      if (typeof onPressEnd === "function") {
        onPressEnd(endEvent, { success });
      }
    };
    const onPointerUp = (upEvent) => {
      onPointerEnd(upEvent, options.useGlobalTarget || isNodeOrChild.isNodeOrChild(element, upEvent.target));
    };
    const onPointerCancel = (cancelEvent) => {
      onPointerEnd(cancelEvent, false);
    };
    window.addEventListener("pointerup", onPointerUp, eventOptions);
    window.addEventListener("pointercancel", onPointerCancel, eventOptions);
  };
  elements.forEach((element) => {
    if (!isKeyboardAccessible.isElementKeyboardAccessible(element) && element.getAttribute("tabindex") === null) {
      element.tabIndex = 0;
    }
    const target = options.useGlobalTarget ? window : element;
    target.addEventListener("pointerdown", startPress, eventOptions);
    element.addEventListener("focus", (event) => keyboard.enableKeyboardPress(event, eventOptions), eventOptions);
  });
  return cancelEvents;
}
exports.press = press;
//# sourceMappingURL=dist-es-gestures-press-index.cjs.map
