"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const globals = require("../utils/globals.cjs");
const objects = require("../utils/objects.cjs");
const useDidUpdateEffect = require("./useDidUpdateEffect.cjs");
const useEventListener = require("./useEventListener.cjs");
const useResizeObserver = require("./useResizeObserver.cjs");
function _interopNamespaceDefault(e) {
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
  if (e) {
    for (const k in e) {
      if (k !== "default") {
        const d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: () => e[k]
        });
      }
    }
  }
  n.default = e;
  return Object.freeze(n);
}
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
function useBoundingClientRect(ref, onChange, listenToScroll = true) {
  var _a;
  const [rect, setRect] = React__namespace.useState(((_a = ref.current) == null ? void 0 : _a.getBoundingClientRect()) || new DOMRect(0, 0, 0, 0));
  const domRectToObject = React__namespace.useCallback((domRect) => {
    const { top, right, bottom, left, width, height, x, y } = domRect;
    return { top, right, bottom, left, width, height, x, y };
  }, []);
  const setRectSize = React__namespace.useCallback(() => {
    if (ref.current) {
      const boundingClientRect = ref.current.getBoundingClientRect();
      if (objects.contentDependency(domRectToObject(boundingClientRect)) !== objects.contentDependency(domRectToObject(rect))) {
        onChange == null ? void 0 : onChange(boundingClientRect);
        setRect(boundingClientRect);
      }
    }
  }, [ref, domRectToObject, rect, onChange]);
  useResizeObserver.useResizeObserver(setRectSize, {}, ref);
  useDidUpdateEffect.useDidUpdateEffect(() => {
    if (listenToScroll) {
      setRectSize();
    }
  }, [listenToScroll]);
  const onScroll = React__namespace.useCallback(() => {
    if (listenToScroll) {
      setRectSize();
    }
  }, [listenToScroll, setRectSize]);
  useEventListener.useEventListener("resize", setRectSize, globals.Document);
  useEventListener.useEventListener("scroll", onScroll, globals.Document, { capture: true });
  React__namespace.useEffect(() => {
    setRectSize();
  }, []);
  return [rect, setRectSize];
}
exports.useBoundingClientRect = useBoundingClientRect;
//# sourceMappingURL=useBoundingClientRect.cjs.map
