"use client";
import * as f from "react";
import { i as H } from "./floating-ui.utils.dom-jr2suqy2.js";
import { u as W } from "./useStableCallback-CudqoENs.js";
import { S as N } from "./safeReact-DKq-628G.js";
import { u as _ } from "./useIsoLayoutEffect-DpJo20by.js";
import { f as $, m as v, d as I } from "./useRenderElement-DNB2vtF1.js";
let C;
process.env.NODE_ENV !== "production" && (C = /* @__PURE__ */ new Set());
function P(...n) {
  if (process.env.NODE_ENV !== "production") {
    const t = n.join(" ");
    C.has(t) || (C.add(t), console.error(`Base UI: ${t}`));
  }
}
const R = /* @__PURE__ */ f.createContext(void 0);
process.env.NODE_ENV !== "production" && (R.displayName = "CompositeRootContext");
function A(n = !1) {
  const t = f.useContext(R);
  if (t === void 0 && !n)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: CompositeRootContext is missing. Composite parts must be placed within <Composite.Root>." : $(16));
  return t;
}
function L(n) {
  const {
    focusableWhenDisabled: t,
    disabled: a,
    composite: p = !1,
    tabIndex: o = 0,
    isNativeButton: c
  } = n, l = p && t !== !1, b = p && t === !1;
  return {
    props: f.useMemo(() => {
      const s = {
        // allow Tabbing away from focusableWhenDisabled elements
        onKeyDown(d) {
          a && t && d.key !== "Tab" && d.preventDefault();
        }
      };
      return p || (s.tabIndex = o, !c && a && (s.tabIndex = t ? o : -1)), (c && (t || l) || !c && a) && (s["aria-disabled"] = a), c && (!t || b) && (s.disabled = a), s;
    }, [p, a, t, l, b, c, o])
  };
}
function X(n = {}) {
  const {
    disabled: t = !1,
    focusableWhenDisabled: a,
    tabIndex: p = 0,
    native: o = !0,
    composite: c
  } = n, l = f.useRef(null), b = A(!0), i = c ?? b !== void 0, {
    props: s
  } = L({
    focusableWhenDisabled: a,
    disabled: t,
    composite: i,
    tabIndex: p,
    isNativeButton: o
  });
  process.env.NODE_ENV !== "production" && f.useEffect(() => {
    if (!l.current)
      return;
    const r = g(l.current);
    if (o) {
      if (!r) {
        const u = N.captureOwnerStack?.() || "";
        P(`A component that acts as a button expected a native <button> because the \`nativeButton\` prop is true. Rendering a non-<button> removes native button semantics, which can impact forms and accessibility. Use a real <button> in the \`render\` prop, or set \`nativeButton\` to \`false\`.${u}`);
      }
    } else if (r) {
      const u = N.captureOwnerStack?.() || "";
      P(`A component that acts as a button expected a non-<button> because the \`nativeButton\` prop is false. Rendering a <button> keeps native behavior while Base UI applies non-native attributes and handlers, which can add unintended extra attributes (such as \`role\` or \`aria-disabled\`). Use a non-<button> in the \`render\` prop, or set \`nativeButton\` to \`true\`.${u}`);
    }
  }, [o]);
  const d = f.useCallback(() => {
    const r = l.current;
    g(r) && i && t && s.disabled === void 0 && r.disabled && (r.disabled = !1);
  }, [t, s.disabled, i]);
  _(d, [d]);
  const O = f.useCallback((r = {}) => {
    const {
      onClick: u,
      onMouseDown: D,
      onKeyUp: U,
      onKeyDown: K,
      onPointerDown: S,
      ...M
    } = r;
    return v({
      type: o ? "button" : void 0,
      onClick(e) {
        if (t) {
          e.preventDefault();
          return;
        }
        u?.(e);
      },
      onMouseDown(e) {
        t || D?.(e);
      },
      onKeyDown(e) {
        if (t || (I(e), K?.(e), e.baseUIHandlerPrevented))
          return;
        const x = e.target === e.currentTarget, m = e.currentTarget, k = g(m), w = !o && F(m), E = x && (o ? k : !w), y = e.key === "Enter", B = e.key === " ", h = m.getAttribute("role"), V = h?.startsWith("menuitem") || h === "option" || h === "gridcell";
        if (x && i && B) {
          if (e.defaultPrevented && V)
            return;
          e.preventDefault(), w || o && k ? (m.click(), e.preventBaseUIHandler()) : E && (u?.(e), e.preventBaseUIHandler());
          return;
        }
        E && (!o && (B || y) && e.preventDefault(), !o && y && u?.(e));
      },
      onKeyUp(e) {
        if (!t) {
          if (I(e), U?.(e), e.target === e.currentTarget && o && i && g(e.currentTarget) && e.key === " ") {
            e.preventDefault();
            return;
          }
          e.baseUIHandlerPrevented || e.target === e.currentTarget && !o && !i && e.key === " " && u?.(e);
        }
      },
      onPointerDown(e) {
        if (t) {
          e.preventDefault();
          return;
        }
        S?.(e);
      }
    }, o ? void 0 : {
      role: "button"
    }, s, M);
  }, [t, s, i, o]), T = W((r) => {
    l.current = r, d();
  });
  return {
    getButtonProps: O,
    buttonRef: T
  };
}
function g(n) {
  return H(n) && n.tagName === "BUTTON";
}
function F(n) {
  return !!(n?.tagName === "A" && n?.href);
}
export {
  R as C,
  X as a,
  P as e,
  A as u
};
