import { a as d, i as S, b as y, E, _ as p, c as O, p as P, d as T, C as m, t as I, S as N } from "./actions-fe213e88.esm.mjs";
function j(t, e) {
  if (t == null)
    return {};
  var s = {}, n = Object.keys(t), r, o;
  for (o = 0; o < n.length; o++)
    r = n[o], !(e.indexOf(r) >= 0) && (s[r] = t[r]);
  return s;
}
function M(t, e) {
  if (t == null)
    return {};
  var s = j(t, e), n, r;
  if (Object.getOwnPropertySymbols) {
    var o = Object.getOwnPropertySymbols(t);
    for (r = 0; r < o.length; r++)
      n = o[r], !(e.indexOf(n) >= 0) && Object.prototype.propertyIsEnumerable.call(t, n) && (s[n] = t[n]);
  }
  return s;
}
const k = {
  target(t) {
    if (t)
      return () => "current" in t ? t.current : t;
  },
  enabled(t = !0) {
    return t;
  },
  window(t = N.isBrowser ? window : void 0) {
    return t;
  },
  eventOptions({
    passive: t = !0,
    capture: e = !1
  } = {}) {
    return {
      passive: t,
      capture: e
    };
  },
  transform(t) {
    return t;
  }
}, D = ["target", "eventOptions", "window", "enabled", "transform"];
function w(t = {}, e) {
  const s = {};
  for (const [n, r] of Object.entries(e))
    switch (typeof r) {
      case "function":
        if (process.env.NODE_ENV === "development") {
          const o = r.call(s, t[n], n, t);
          Number.isNaN(o) || (s[n] = o);
        } else
          s[n] = r.call(s, t[n], n, t);
        break;
      case "object":
        s[n] = w(t[n], r);
        break;
      case "boolean":
        r && (s[n] = t[n]);
        break;
    }
  return s;
}
function H(t, e, s = {}) {
  const n = t, {
    target: r,
    eventOptions: o,
    window: i,
    enabled: u,
    transform: h
  } = n, a = M(n, D);
  if (s.shared = w({
    target: r,
    eventOptions: o,
    window: i,
    enabled: u,
    transform: h
  }, k), e) {
    const c = m.get(e);
    s[e] = w(p({
      shared: s.shared
    }, a), c);
  } else
    for (const c in a) {
      const v = m.get(c);
      if (v)
        s[c] = w(p({
          shared: s.shared
        }, a[c]), v);
      else if (process.env.NODE_ENV === "development" && !["drag", "pinch", "scroll", "wheel", "move", "hover"].includes(c)) {
        if (c === "domTarget")
          throw Error("[@use-gesture]: `domTarget` option has been renamed to `target`.");
        console.warn(`[@use-gesture]: Unknown config key \`${c}\` was used. Please read the documentation for further information.`);
      }
    }
  return s;
}
class _ {
  constructor(e, s) {
    d(this, "_listeners", /* @__PURE__ */ new Set()), this._ctrl = e, this._gestureKey = s;
  }
  add(e, s, n, r, o) {
    const i = this._listeners, u = I(s, n), h = this._gestureKey ? this._ctrl.config[this._gestureKey].eventOptions : {}, a = p(p({}, h), o);
    e.addEventListener(u, r, a);
    const c = () => {
      e.removeEventListener(u, r, a), i.delete(c);
    };
    return i.add(c), c;
  }
  clean() {
    this._listeners.forEach((e) => e()), this._listeners.clear();
  }
}
class R {
  constructor() {
    d(this, "_timeouts", /* @__PURE__ */ new Map());
  }
  add(e, s, n = 140, ...r) {
    this.remove(e), this._timeouts.set(e, window.setTimeout(s, n, ...r));
  }
  remove(e) {
    const s = this._timeouts.get(e);
    s && window.clearTimeout(s);
  }
  clean() {
    this._timeouts.forEach((e) => void window.clearTimeout(e)), this._timeouts.clear();
  }
}
class G {
  constructor(e) {
    d(this, "gestures", /* @__PURE__ */ new Set()), d(this, "_targetEventStore", new _(this)), d(this, "gestureEventStores", {}), d(this, "gestureTimeoutStores", {}), d(this, "handlers", {}), d(this, "config", {}), d(this, "pointerIds", /* @__PURE__ */ new Set()), d(this, "touchIds", /* @__PURE__ */ new Set()), d(this, "state", {
      shared: {
        shiftKey: !1,
        metaKey: !1,
        ctrlKey: !1,
        altKey: !1
      }
    }), W(this, e);
  }
  setEventIds(e) {
    if (S(e))
      return this.touchIds = new Set(y(e)), this.touchIds;
    if ("pointerId" in e)
      return e.type === "pointerup" || e.type === "pointercancel" ? this.pointerIds.delete(e.pointerId) : e.type === "pointerdown" && this.pointerIds.add(e.pointerId), this.pointerIds;
  }
  applyHandlers(e, s) {
    this.handlers = e, this.nativeHandlers = s;
  }
  applyConfig(e, s) {
    this.config = H(e, s, this.config);
  }
  clean() {
    this._targetEventStore.clean();
    for (const e of this.gestures)
      this.gestureEventStores[e].clean(), this.gestureTimeoutStores[e].clean();
  }
  effect() {
    return this.config.shared.target && this.bind(), () => this._targetEventStore.clean();
  }
  bind(...e) {
    const s = this.config.shared, n = {};
    let r;
    if (!(s.target && (r = s.target(), !r))) {
      if (s.enabled) {
        for (const i of this.gestures) {
          const u = this.config[i], h = b(n, u.eventOptions, !!r);
          if (u.enabled) {
            const a = E.get(i);
            new a(this, e, i).bind(h);
          }
        }
        const o = b(n, s.eventOptions, !!r);
        for (const i in this.nativeHandlers)
          o(i, "", (u) => this.nativeHandlers[i](p(p({}, this.state.shared), {}, {
            event: u,
            args: e
          })), void 0, !0);
      }
      for (const o in n)
        n[o] = O(...n[o]);
      if (!r)
        return n;
      for (const o in n) {
        const {
          device: i,
          capture: u,
          passive: h
        } = P(o);
        this._targetEventStore.add(r, i, "", n[o], {
          capture: u,
          passive: h
        });
      }
    }
  }
}
function l(t, e) {
  t.gestures.add(e), t.gestureEventStores[e] = new _(t, e), t.gestureTimeoutStores[e] = new R();
}
function W(t, e) {
  e.drag && l(t, "drag"), e.wheel && l(t, "wheel"), e.scroll && l(t, "scroll"), e.move && l(t, "move"), e.pinch && l(t, "pinch"), e.hover && l(t, "hover");
}
const b = (t, e, s) => (n, r, o, i = {}, u = !1) => {
  var h, a;
  const c = (h = i.capture) !== null && h !== void 0 ? h : e.capture, v = (a = i.passive) !== null && a !== void 0 ? a : e.passive;
  let g = u ? n : T(n, r, c);
  s && v && (g += "Passive"), t[g] = t[g] || [], t[g].push(o);
}, $ = /^on(Drag|Wheel|Scroll|Move|Pinch|Hover)/;
function x(t) {
  const e = {}, s = {}, n = /* @__PURE__ */ new Set();
  for (let r in t)
    $.test(r) ? (n.add(RegExp.lastMatch), s[r] = t[r]) : e[r] = t[r];
  return [s, e, n];
}
function f(t, e, s, n, r, o) {
  if (!t.has(s))
    return;
  if (!E.has(n)) {
    process.env.NODE_ENV === "development" && console.warn(`[@use-gesture]: You've created a custom handler that that uses the \`${n}\` gesture but isn't properly configured.

Please add \`${n}Action\` when creating your handler.`);
    return;
  }
  const i = s + "Start", u = s + "End", h = (a) => {
    let c;
    return a.first && i in e && e[i](a), s in e && (c = e[s](a)), a.last && u in e && e[u](a), c;
  };
  r[n] = h, o[n] = o[n] || {};
}
function L(t, e) {
  const [s, n, r] = x(t), o = {};
  return f(r, s, "onDrag", "drag", o, e), f(r, s, "onWheel", "wheel", o, e), f(r, s, "onScroll", "scroll", o, e), f(r, s, "onPinch", "pinch", o, e), f(r, s, "onMove", "move", o, e), f(r, s, "onHover", "hover", o, e), {
    handlers: o,
    config: e,
    nativeHandlers: n
  };
}
export {
  G as Controller,
  L as parseMergedHandlers
};
