"use client";
import { __spreadArray as x } from "../../../tslib/tslib.es6.js";
import * as n from "react";
import { RemoveScrollBar as A } from "../../../react-remove-scroll-bar/dist/es2015/component.js";
import { styleSingleton as I } from "../../../react-style-singleton/dist/es2015/component.js";
import { nonPassive as s } from "./aggresiveCapture.js";
import { locationCouldBeScrolled as T, handleScroll as W } from "./handleScroll.js";
var b = function(e) {
  return "changedTouches" in e ? [e.changedTouches[0].clientX, e.changedTouches[0].clientY] : [0, 0];
}, Y = function(e) {
  return [e.deltaX, e.deltaY];
}, B = function(e) {
  return e && "current" in e ? e.current : e;
}, F = function(e, c) {
  return e[0] === c[0] && e[1] === c[1];
}, K = function(e) {
  return `
  .block-interactivity-`.concat(e, ` {pointer-events: none;}
  .allow-interactivity-`).concat(e, ` {pointer-events: all;}
`);
}, O = 0, f = [];
function G(e) {
  var c = n.useRef([]), E = n.useRef([0, 0]), d = n.useRef(), h = n.useState(O++)[0], m = n.useState(I)[0], g = n.useRef(e);
  n.useEffect(function() {
    g.current = e;
  }, [e]), n.useEffect(function() {
    if (e.inert) {
      document.body.classList.add("block-interactivity-".concat(h));
      var t = x([e.lockRef.current], (e.shards || []).map(B), !0).filter(Boolean);
      return t.forEach(function(r) {
        return r.classList.add("allow-interactivity-".concat(h));
      }), function() {
        document.body.classList.remove("block-interactivity-".concat(h)), t.forEach(function(r) {
          return r.classList.remove("allow-interactivity-".concat(h));
        });
      };
    }
  }, [e.inert, e.lockRef.current, e.shards]);
  var k = n.useCallback(function(t, r) {
    if ("touches" in t && t.touches.length === 2 || t.type === "wheel" && t.ctrlKey)
      return !g.current.allowPinchZoom;
    var l = b(t), o = E.current, u = "deltaX" in t ? t.deltaX : o[0] - l[0], i = "deltaY" in t ? t.deltaY : o[1] - l[1], a, C = t.target, v = Math.abs(u) > Math.abs(i) ? "h" : "v";
    if ("touches" in t && v === "h" && C.type === "range")
      return !1;
    var y = T(v, C);
    if (!y)
      return !0;
    if (y ? a = v : (a = v === "v" ? "h" : "v", y = T(v, C)), !y)
      return !1;
    if (!d.current && "changedTouches" in t && (u || i) && (d.current = a), !a)
      return !0;
    var P = d.current || a;
    return W(P, r, t, P === "h" ? u : i);
  }, []), S = n.useCallback(function(t) {
    var r = t;
    if (!(!f.length || f[f.length - 1] !== m)) {
      var l = "deltaY" in r ? Y(r) : b(r), o = c.current.filter(function(a) {
        return a.name === r.type && (a.target === r.target || r.target === a.shadowParent) && F(a.delta, l);
      })[0];
      if (o && o.should) {
        r.cancelable && r.preventDefault();
        return;
      }
      if (!o) {
        var u = (g.current.shards || []).map(B).filter(Boolean).filter(function(a) {
          return a.contains(r.target);
        }), i = u.length > 0 ? k(r, u[0]) : !g.current.noIsolation;
        i && r.cancelable && r.preventDefault();
      }
    }
  }, []), R = n.useCallback(function(t, r, l, o) {
    var u = { name: t, delta: r, target: l, should: o, shadowParent: Q(l) };
    c.current.push(u), setTimeout(function() {
      c.current = c.current.filter(function(i) {
        return i !== u;
      });
    }, 1);
  }, []), w = n.useCallback(function(t) {
    E.current = b(t), d.current = void 0;
  }, []), L = n.useCallback(function(t) {
    R(t.type, Y(t), t.target, k(t, e.lockRef.current));
  }, []), M = n.useCallback(function(t) {
    R(t.type, b(t), t.target, k(t, e.lockRef.current));
  }, []);
  n.useEffect(function() {
    return f.push(m), e.setCallbacks({
      onScrollCapture: L,
      onWheelCapture: L,
      onTouchMoveCapture: M
    }), document.addEventListener("wheel", S, s), document.addEventListener("touchmove", S, s), document.addEventListener("touchstart", w, s), function() {
      f = f.filter(function(t) {
        return t !== m;
      }), document.removeEventListener("wheel", S, s), document.removeEventListener("touchmove", S, s), document.removeEventListener("touchstart", w, s);
    };
  }, []);
  var X = e.removeScrollBar, D = e.inert;
  return n.createElement(
    n.Fragment,
    null,
    D ? n.createElement(m, { styles: K(h) }) : null,
    X ? n.createElement(A, { noRelative: e.noRelative, gapMode: e.gapMode }) : null
  );
}
function Q(e) {
  for (var c = null; e !== null; )
    e instanceof ShadowRoot && (c = e.host, e = e.host), e = e.parentNode;
  return c;
}
export {
  G as RemoveScrollSideCar,
  Y as getDeltaXY,
  b as getTouchXY
};
