import * as n from "react";
import { useLayoutEffect as v } from "../../../../../@radix-ui_react-use-layout-effect@1.1.1_@types_react@19.0.10_react@19.0.0/node_modules/@radix-ui/react-use-layout-effect/dist/index.js";
var E = n[" useInsertionEffect ".trim().toString()] || v;
function y({
  prop: t,
  defaultProp: u,
  onChange: o = () => {
  },
  caller: i
}) {
  const [l, e, r] = w({
    defaultProp: u,
    onChange: o
  }), c = t !== void 0, a = c ? t : l;
  {
    const s = n.useRef(t !== void 0);
    n.useEffect(() => {
      const f = s.current;
      f !== c && console.warn(
        `${i} is changing from ${f ? "controlled" : "uncontrolled"} to ${c ? "controlled" : "uncontrolled"}. Components should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled value for the lifetime of the component.`
      ), s.current = c;
    }, [c, i]);
  }
  const m = n.useCallback(
    (s) => {
      var f;
      if (c) {
        const d = R(s) ? s(t) : s;
        d !== t && ((f = r.current) == null || f.call(r, d));
      } else
        e(s);
    },
    [c, t, e, r]
  );
  return [a, m];
}
function w({
  defaultProp: t,
  onChange: u
}) {
  const [o, i] = n.useState(t), l = n.useRef(o), e = n.useRef(u);
  return E(() => {
    e.current = u;
  }, [u]), n.useEffect(() => {
    var r;
    l.current !== o && ((r = e.current) == null || r.call(e, o), l.current = o);
  }, [o, l]), [o, i, e];
}
function R(t) {
  return typeof t == "function";
}
export {
  y as useControllableState
};
//# sourceMappingURL=index.js.map
