import D from "../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import $ from "../../../@babel/runtime/helpers/esm/typeof.mjs";
import h from "../../../rc-util/es/Dom/findDOMNode.mjs";
import { supportRef as j, useComposeRef as B } from "../../../rc-util/es/ref.mjs";
import * as e from "react";
import { CollectionContext as P } from "../Collection.mjs";
import { observe as V, unobserve as k } from "../utils/observerUtil.mjs";
import I from "./DomWrapper.mjs";
function q(f, M) {
  var l = f.children, v = f.disabled, t = e.useRef(null), m = e.useRef(null), d = e.useContext(P), R = typeof l == "function", o = R ? l(t) : l, i = e.useRef({
    width: -1,
    height: -1,
    offsetWidth: -1,
    offsetHeight: -1
  }), p = !R && /* @__PURE__ */ e.isValidElement(o) && j(o), O = p ? o.ref : null, N = B(O, t), g = function() {
    var n;
    return h(t.current) || // Support `nativeElement` format
    (t.current && $(t.current) === "object" ? h((n = t.current) === null || n === void 0 ? void 0 : n.nativeElement) : null) || h(m.current);
  };
  e.useImperativeHandle(M, function() {
    return g();
  });
  var b = e.useRef(f);
  b.current = f;
  var C = e.useCallback(function(r) {
    var n = b.current, E = n.onResize, S = n.data, H = r.getBoundingClientRect(), s = H.width, c = H.height, u = r.offsetWidth, a = r.offsetHeight, W = Math.floor(s), w = Math.floor(c);
    if (i.current.width !== W || i.current.height !== w || i.current.offsetWidth !== u || i.current.offsetHeight !== a) {
      var _ = {
        width: W,
        height: w,
        offsetWidth: u,
        offsetHeight: a
      };
      i.current = _;
      var y = u === Math.round(s) ? s : u, z = a === Math.round(c) ? c : a, x = D(D({}, _), {}, {
        offsetWidth: y,
        offsetHeight: z
      });
      d == null || d(x, r, S), E && Promise.resolve().then(function() {
        E(x, r);
      });
    }
  }, []);
  return e.useEffect(function() {
    var r = g();
    return r && !v && V(r, C), function() {
      return k(r, C);
    };
  }, [t.current, v]), /* @__PURE__ */ e.createElement(I, {
    ref: m
  }, p ? /* @__PURE__ */ e.cloneElement(o, {
    ref: N
  }) : o);
}
var A = /* @__PURE__ */ e.forwardRef(q);
process.env.NODE_ENV !== "production" && (A.displayName = "SingleObserver");
export {
  A as default
};
