import { o as M } from "./rolldown-runtime.js";
import { i as N } from "./vendor/react.js";
import { p as R } from "./safeReact.js";
import { c as T, l as O, s as H } from "./useRenderElement.js";
import { n as W, t as L } from "./useIsoLayoutEffect.js";
var c = /* @__PURE__ */ M(N()), _ = /* @__PURE__ */ c.createContext(void 0);
function F(n = !1) {
  const t = c.useContext(_);
  if (t === void 0 && !n) throw new Error(H(16));
  return t;
}
function S(n) {
  const { focusableWhenDisabled: t, disabled: s, composite: l = !1, tabIndex: o = 0, isNativeButton: a } = n, f = l && t !== !1, d = l && t === !1;
  return { props: c.useMemo(() => {
    const r = { onKeyDown(i) {
      s && t && i.key !== "Tab" && i.preventDefault();
    } };
    return l || (r.tabIndex = o, !a && s && (r.tabIndex = t ? o : -1)), (a && (t || f) || !a && s) && (r["aria-disabled"] = s), a && (!t || d) && (r.disabled = s), r;
  }, [
    l,
    s,
    t,
    f,
    d,
    a,
    o
  ]) };
}
function J(n = {}) {
  const { disabled: t = !1, focusableWhenDisabled: s, tabIndex: l = 0, native: o = !0, composite: a } = n, f = c.useRef(null), d = F(!0), r = a ?? d !== void 0, { props: i } = S({
    focusableWhenDisabled: s,
    disabled: t,
    composite: r,
    tabIndex: l,
    isNativeButton: o
  }), m = c.useCallback(() => {
    const u = f.current;
    x(u) && r && t && i.disabled === void 0 && u.disabled && (u.disabled = !1);
  }, [
    t,
    i.disabled,
    r
  ]);
  return L(m, [m]), {
    getButtonProps: c.useCallback((u = {}) => {
      const { onClick: p, onMouseDown: E, onKeyUp: I, onKeyDown: B, onPointerDown: h, ...K } = u;
      return O({
        type: o ? "button" : void 0,
        onClick(e) {
          if (t) {
            e.preventDefault();
            return;
          }
          p?.(e);
        },
        onMouseDown(e) {
          t || E?.(e);
        },
        onKeyDown(e) {
          if (t || (T(e), B?.(e), e.baseUIHandlerPrevented)) return;
          const g = e.target === e.currentTarget, b = e.currentTarget, C = x(b), k = !o && A(b), y = g && (o ? C : !k), P = e.key === "Enter", w = e.key === " ", D = b.getAttribute("role"), U = D?.startsWith("menuitem") || D === "option" || D === "gridcell";
          if (g && r && w) {
            if (e.defaultPrevented && U) return;
            e.preventDefault(), k || o && C ? (b.click(), e.preventBaseUIHandler()) : y && (p?.(e), e.preventBaseUIHandler());
            return;
          }
          y && (!o && (w || P) && e.preventDefault(), !o && P && p?.(e));
        },
        onKeyUp(e) {
          if (!t) {
            if (T(e), I?.(e), e.target === e.currentTarget && o && r && x(e.currentTarget) && e.key === " ") {
              e.preventDefault();
              return;
            }
            e.baseUIHandlerPrevented || e.target === e.currentTarget && !o && !r && e.key === " " && p?.(e);
          }
        },
        onPointerDown(e) {
          if (t) {
            e.preventDefault();
            return;
          }
          h?.(e);
        }
      }, o ? void 0 : { role: "button" }, i, K);
    }, [
      t,
      i,
      r,
      o
    ]),
    buttonRef: W((u) => {
      f.current = u, m();
    })
  };
}
function x(n) {
  return R(n) && n.tagName === "BUTTON";
}
function A(n) {
  return !!(n?.tagName === "A" && n?.href);
}
export {
  _ as n,
  F as r,
  J as t
};

//# sourceMappingURL=useButton.js.map