import * as c from "react";
import { useRefWithInit as r } from "./useRefWithInit.js";
const n = c[`useInsertionEffect${Math.random().toFixed(1)}`.slice(0, -3)], o = (
  // React 17 doesn't have useInsertionEffect.
  n && // Preact replaces useInsertionEffect with useLayoutEffect and fires too late.
  n !== c.useLayoutEffect ? n : (e) => e()
);
function i(e) {
  const t = r(f).current;
  return t.next = e, o(t.effect), t.trampoline;
}
function f() {
  const e = {
    next: void 0,
    callback: l,
    trampoline: (...t) => {
      var a;
      return (a = e.callback) == null ? void 0 : a.call(e, ...t);
    },
    effect: () => {
      e.callback = e.next;
    }
  };
  return e;
}
function l() {
  if (process.env.NODE_ENV !== "production")
    throw new Error("Base UI: Cannot call an event handler while rendering.");
}
export {
  i as useStableCallback
};
