import { o as i } from "./rolldown-runtime.js";
import { i as m, r as b } from "./vendor/react.js";
import { n as s } from "./useIsoLayoutEffect.js";
import { n as p } from "./shadowDom.js";
import { I as g, L as T, P as d, R as E } from "./popupStateMapping.js";
import { c as n, t as c } from "./createBaseUIEventDetails.js";
var F = /* @__PURE__ */ i(m()), u = /* @__PURE__ */ i(b());
function O(r, l) {
  const o = F.useRef(null);
  return {
    preFocusGuardRef: o,
    handlePreFocusGuardFocus: s((t) => {
      u.flushSync(() => {
        r.setOpen(!1, c(n, t.nativeEvent, t.currentTarget));
      }), T(o.current)?.focus();
    }),
    handleFocusTargetFocus: s((t) => {
      const a = r.select("positionerElement");
      if (a && E(t, a)) r.context.beforeContentFocusGuardRef.current?.focus();
      else {
        u.flushSync(() => {
          r.setOpen(!1, c(n, t.nativeEvent, t.currentTarget));
        });
        let e = g(r.context.triggerFocusTargetRef.current || l.current);
        for (; e !== null && p(a, e); ) {
          const f = e;
          if (e = d(e), e === f) break;
        }
        e?.focus();
      }
    })
  };
}
export {
  O as t
};

//# sourceMappingURL=useTriggerFocusGuards.js.map