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