import { useState as A, useRef as z, useMemo as F, useLayoutEffect as N } from "react";
import a from "../../../hooks/useResizeObserver.js";
const j = 1, x = (t) => {
  const e = t?.trim().match(/^(-?\d+(?:\.\d+)?)px$/);
  return e ? Number(e[1]) : void 0;
}, C = (t) => {
  const e = t.getBoundingClientRect().width;
  return e > 0 ? Math.ceil(e) : t.scrollWidth;
}, P = (t) => {
  if (!t)
    return 0;
  const e = window.getComputedStyle(t), n = Number.parseFloat(e.columnGap || e.gap || "0");
  return Number.isFinite(n) ? Math.ceil(n) : 0;
}, T = (t) => {
  const e = t.querySelector(".table-head-label");
  if (e) {
    const o = t.querySelector(".sort-arrows");
    return C(e) + (o ? C(o) : 0) + (o ? P(t.querySelector(".table-head-button")) : 0);
  }
  return (t.querySelector(".table-cell-content, .table-head-button-label") ?? t).scrollWidth;
}, _ = (t, e) => Object.fromEntries(
  Object.entries(e).map(([n, o]) => {
    const u = t[n], i = x(u), r = x(o);
    return [
      n,
      i !== void 0 && r !== void 0 && Math.abs(i - r) <= j ? u : o
    ];
  })
), D = (t) => {
  const { columns: e, enabled: n, tableWrapperRef: o } = t, [u, i] = A({}), r = z(""), c = z({}), g = F(
    () => e.map((l) => `${l.key}:${l.width === void 0 ? "auto" : String(l.width)}`).join("|"),
    [e]
  ), s = o.current, v = s?.querySelector(".table-head") ?? null, w = s?.querySelector(".table-body") ?? null, R = s?.querySelector(".table-footer") ?? null, q = s?.querySelector(".table-scroll-content") ?? null, [, , b] = a(s), [, , S] = a(v), [, , M] = a(w), [, , f] = a(R), [, , p] = a(q);
  return N(() => {
    if (!n || !s) {
      r.current && (r.current = "", c.current = {}, i({}));
      return;
    }
    if (e.length === 0) {
      r.current && (r.current = "", c.current = {}, i({}));
      return;
    }
    const l = e.filter((d) => d.width === void 0);
    if (l.length === 0) {
      r.current && (r.current = "", c.current = {}, i({}));
      return;
    }
    (() => {
      const d = Array.from(s.querySelectorAll("[data-column]")), k = Object.fromEntries(
        l.flatMap((E) => {
          const y = d.filter((h) => h.dataset.column === E.key).reduce(
            (h, O) => Math.max(h, T(O)),
            0
          );
          return y > 0 ? [[E.key, `${Math.ceil(y)}px`]] : [];
        })
      ), m = _(
        c.current,
        k
      ), W = JSON.stringify(m);
      W !== r.current && (r.current = W, c.current = m, i(m));
    })();
  }, [
    g,
    n,
    s,
    b.blockSize,
    b.inlineSize,
    S.blockSize,
    S.inlineSize,
    M.blockSize,
    M.inlineSize,
    f.blockSize,
    f.inlineSize,
    p.blockSize,
    p.inlineSize
  ]), u;
};
export {
  _ as stabilizeMeasuredColumnMaxWidths,
  D as useMeasuredColumnMaxWidths
};
//# sourceMappingURL=useMeasuredColumnMaxWidths.js.map
