import { useTouch as E } from "./use-touch.mjs";
import { useEffect as L } from "react";
import { getScrollParent as b } from "./get-scroll-parent.mjs";
import { supportsPassive as m } from "./supports-passive.mjs";
let r = 0;
const f = "adm-overflow-hidden";
function S(o) {
  let t = o == null ? void 0 : o.parentElement;
  for (; t; ) {
    if (t.clientHeight < t.scrollHeight)
      return t;
    t = t.parentElement;
  }
  return null;
}
function P(o, t) {
  const n = E(), i = (e) => {
    n.move(e);
    const h = n.deltaY.current > 0 ? "10" : "01", s = b(e.target, o.current);
    if (!s)
      return;
    if (t === "strict") {
      const a = S(e.target);
      if (a === document.body || a === document.documentElement) {
        e.preventDefault();
        return;
      }
    }
    const {
      scrollHeight: l,
      offsetHeight: v,
      scrollTop: u
    } = s, {
      height: g
    } = s.getBoundingClientRect();
    let c = "11";
    u === 0 ? c = v >= l ? "00" : "01" : l <= Math.round(g + u) && (c = "10"), c !== "11" && n.isVertical() && !(parseInt(c, 2) & parseInt(h, 2)) && e.cancelable && m && e.preventDefault();
  }, d = () => {
    document.addEventListener("touchstart", n.start), document.addEventListener("touchmove", i, m ? {
      passive: !1
    } : !1), r || document.body.classList.add(f), r++;
  }, p = () => {
    r && (document.removeEventListener("touchstart", n.start), document.removeEventListener("touchmove", i), r--, r || document.body.classList.remove(f));
  };
  L(() => {
    if (t)
      return d(), () => {
        p();
      };
  }, [t]);
}
export {
  P as useLockScroll
};
