import * as i from "react";
import { isHTMLElement as I } from "../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { useStableCallback as E } from "../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useStableCallback.js";
import { error as D } from "../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/error.js";
import { useIsoLayoutEffect as R } from "../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useIsoLayoutEffect.js";
import { useCompositeRootContext as U } from "../composite/root/CompositeRootContext.js";
import { useFocusableWhenDisabled as C } from "../utils/useFocusableWhenDisabled.js";
import { mergeProps as K, makeEventPreventable as g } from "../merge-props/mergeProps.js";
function j(a = {}) {
  const {
    disabled: o = !1,
    focusableWhenDisabled: y,
    tabIndex: B = 0,
    native: r = !0
  } = a, s = i.useRef(null), f = U(!0) !== void 0, b = E(() => {
    const e = s.current;
    return !!((e == null ? void 0 : e.tagName) === "A" && (e != null && e.href));
  }), {
    props: u
  } = C({
    focusableWhenDisabled: y,
    disabled: o,
    composite: f,
    tabIndex: B,
    isNativeButton: r
  });
  process.env.NODE_ENV !== "production" && i.useEffect(() => {
    if (!s.current)
      return;
    const e = s.current.tagName === "BUTTON";
    r ? e || D("A component that acts as a button was not rendered as a native <button>, which does not match the default. Ensure that the element passed to the `render` prop of the component is a real <button>, or set the `nativeButton` prop on the component to `false`.") : e && D("A component that acts as a button was rendered as a native <button>, which does not match the default. Ensure that the element passed to the `render` prop of the component is not a real <button>, or set the `nativeButton` prop on the component to `true`.");
  }, [r]);
  const c = i.useCallback(() => {
    const e = s.current;
    A(e) && f && o && u.disabled === void 0 && e.disabled && (e.disabled = !1);
  }, [o, u.disabled, f]);
  R(c, [c]);
  const N = i.useCallback((e = {}) => {
    const {
      onClick: n,
      onMouseDown: p,
      onKeyUp: d,
      onKeyDown: m,
      onPointerDown: l,
      ...T
    } = e;
    return K({
      type: r ? "button" : void 0,
      onClick(t) {
        if (o) {
          t.preventDefault();
          return;
        }
        n == null || n(t);
      },
      onMouseDown(t) {
        o || p == null || p(t);
      },
      onKeyDown(t) {
        if (o || (g(t), m == null || m(t)), t.baseUIHandlerPrevented)
          return;
        const w = t.target === t.currentTarget && !r && !b() && !o, h = t.key === "Enter", k = t.key === " ";
        w && ((k || h) && t.preventDefault(), h && (n == null || n(t)));
      },
      onKeyUp(t) {
        o || (g(t), d == null || d(t)), !t.baseUIHandlerPrevented && t.target === t.currentTarget && !r && !o && t.key === " " && (n == null || n(t));
      },
      onPointerDown(t) {
        if (o) {
          t.preventDefault();
          return;
        }
        l == null || l(t);
      }
    }, r ? void 0 : {
      role: "button"
    }, u, T);
  }, [o, u, r, b]), P = E((e) => {
    s.current = e, c();
  });
  return {
    getButtonProps: N,
    buttonRef: P
  };
}
function A(a) {
  return I(a) && a.tagName === "BUTTON";
}
export {
  j as useButton
};
