import { useState as H, useRef as d, useCallback as A, useLayoutEffect as v, useEffect as M } from "react";
import { DEFAULT_TABLE_HEIGHT as E } from "../constants/tableDefaults.js";
import x from "../utils/hasBoundedHeight.js";
import F from "../utils/calculateSiblingsAndGaps.js";
import T from "../utils/isFeedbackLoop.js";
const s = 0.7, R = ({
  outerRef: u,
  tableContainerRef: g,
  enabled: o,
  minHeight: r = E
}) => {
  const [h, i] = H(() => {
    if (o)
      return typeof window > "u" ? E : Math.max(Math.floor(window.innerHeight * s), r);
  }), l = d(0), m = d(!1), p = d(h);
  p.current = h;
  const c = A(() => {
    const e = u.current, n = g.current;
    if (!e || !n) return;
    if (!x(e.parentElement)) {
      process.env.NODE_ENV !== "production" && !m.current && (console.warn(
        `[DataTable] tableHeight="auto" requires a parent element with a bounded height (e.g. h-screen, height: 100vh, max-height, or a flex/grid container with constrained parent). Falling back to ${Math.floor(window.innerHeight * s)}px. For large datasets without a bounded container, use tableHeight={number} instead.`
      ), m.current = !0), i(Math.max(Math.floor(window.innerHeight * s), r));
      return;
    }
    const t = e.getBoundingClientRect().height;
    if (t > window.innerHeight * 1.1 || t <= 0) {
      i(Math.max(Math.floor(window.innerHeight * s), r));
      return;
    }
    const { siblingsHeight: a, totalGap: w } = F(e, n), b = t - a - w;
    if (T(t, b, a, w, p.current)) {
      i(Math.max(Math.floor(window.innerHeight * s), r));
      return;
    }
    i(Math.max(b, r));
  }, [u, g, r]);
  return v(() => {
    o && c();
  }, [o, c]), M(() => {
    if (!o) {
      i(void 0);
      return;
    }
    const e = u.current;
    if (!e) return;
    const n = new ResizeObserver(() => {
      cancelAnimationFrame(l.current), l.current = requestAnimationFrame(c);
    });
    if (n.observe(e), e.parentElement) {
      n.observe(e.parentElement);
      const f = e.parentElement.children;
      for (let t = 0; t < f.length; t++) {
        const a = f[t];
        a !== e && n.observe(a);
      }
    }
    return () => {
      cancelAnimationFrame(l.current), n.disconnect();
    };
  }, [o, u, c]), h;
};
export {
  R as default
};
//# sourceMappingURL=useAutoTableHeight.js.map
