import { useRef as s, useEffect as i, useCallback as l } from "react";
function y(e, { resize: t = !1 } = {}) {
  const n = s(e);
  i(() => {
    n.current = e;
  }, [e]);
  const R = s(t);
  i(() => {
    R.current = t, a();
  }, [t]);
  const r = s(null), b = l((o) => {
    o !== r.current && (r.current = o, c(), a());
  }, []), h = s(null), c = l(
    ({ force: o = !1 } = {}) => {
      if (!r.current)
        return;
      const f = r.current.getBoundingClientRect();
      if (o || O(h.current, f)) {
        h.current = f;
        const { bottom: g, height: m, left: v, right: d, top: p, width: E, x: w, y: x } = f;
        n.current({
          bottom: g,
          height: m,
          left: v,
          right: d,
          top: p,
          width: E,
          x: w,
          y: x
        });
      }
    },
    []
  ), u = s(null), a = l(() => {
    u.current && (u.current.disconnect(), u.current = null), r.current && R.current && (u.current = new ResizeObserver(() => c()), u.current.observe(r.current));
  }, [c]);
  return i(c), [b, c];
}
var C = [
  "bottom",
  "height",
  "left",
  "right",
  "top",
  "width",
  "x",
  "y"
];
function O(e, t) {
  return !e || e !== t && C.some((n) => e[n] !== t[n]);
}
export {
  y as useRect
};
