import { disposables as f } from "../../utils/disposables.js";
import { isIOS as u } from "../../utils/platform.js";
function h() {
  return u() ? { before({ doc: a, d: l, meta: c }) {
    function r(s) {
      return c.containers.flatMap((o) => o()).some((o) => o.contains(s));
    }
    l.microTask(() => {
      var s;
      if (window.getComputedStyle(a.documentElement).scrollBehavior !== "auto") {
        let e = f();
        e.style(a.documentElement, "scrollBehavior", "auto"), l.add(() => l.microTask(() => e.dispose()));
      }
      let o = (s = window.scrollY) != null ? s : window.pageYOffset, n = null;
      l.addEventListener(a, "click", (e) => {
        if (e.target instanceof HTMLElement) try {
          let t = e.target.closest("a");
          if (!t) return;
          let { hash: d } = new URL(t.href), i = a.querySelector(d);
          i && !r(i) && (n = i);
        } catch {
        }
      }, !0), l.addEventListener(a, "touchstart", (e) => {
        if (e.target instanceof HTMLElement) if (r(e.target)) {
          let t = e.target;
          for (; t.parentElement && r(t.parentElement); ) t = t.parentElement;
          l.style(t, "overscrollBehavior", "contain");
        } else l.style(e.target, "touchAction", "none");
      }), l.addEventListener(a, "touchmove", (e) => {
        if (e.target instanceof HTMLElement) {
          if (e.target.tagName === "INPUT") return;
          if (r(e.target)) {
            let t = e.target;
            for (; t.parentElement && t.dataset.headlessuiPortal !== "" && !(t.scrollHeight > t.clientHeight || t.scrollWidth > t.clientWidth); ) t = t.parentElement;
            t.dataset.headlessuiPortal === "" && e.preventDefault();
          } else e.preventDefault();
        }
      }, { passive: !1 }), l.add(() => {
        var e;
        let t = (e = window.scrollY) != null ? e : window.pageYOffset;
        o !== t && window.scrollTo(0, o), n && n.isConnected && (n.scrollIntoView({ block: "nearest" }), n = null);
      });
    });
  } } : {};
}
export {
  h as handleIOSLocking
};
