import { randomId as g } from "./index-C_1M7Ag6.js";
function w(e) {
  return Number.isFinite(e) && e > 0 ? e : 1;
}
function x(e) {
  return Math.round(e * 100) / 100;
}
function m(e) {
  if (!(e instanceof HTMLElement)) return null;
  const n = e.getBoundingClientRect();
  return [n.left, n.right, n.top, n.bottom].every(Number.isFinite) ? {
    bottom: x(n.bottom),
    left: x(n.left),
    right: x(n.right),
    top: x(n.top)
  } : null;
}
function f(e, n, t) {
  !Number.isFinite(t) || !e[n] || e[n].push(x(t));
}
function C(e) {
  if (e.length === 0) return null;
  const n = [...e].sort((r, o) => r - o), t = Math.floor(n.length / 2);
  return n.length % 2 === 1 ? n[t] : x((n[t - 1] + n[t]) / 2);
}
function A(e) {
  if (e.length === 0) return [];
  const n = [...e], t = n.findIndex((o) => o !== null);
  if (t < 0) return new Array(e.length).fill(0);
  for (let o = 0; o < t; o += 1)
    n[o] = n[t];
  let r = t;
  for (let o = t + 1; o < n.length; o += 1) {
    if (n[o] === null) continue;
    const s = n[r], u = n[o], c = o - r;
    for (let l = r + 1; l < o; l += 1) {
      const a = l - r;
      n[l] = x(s + (u - s) * a / c);
    }
    r = o;
  }
  for (let o = r + 1; o < n.length; o += 1)
    n[o] = n[r];
  return n.map((o) => x(o ?? 0));
}
function N(e) {
  const n = [...e];
  for (let t = 1; t < n.length; t += 1)
    n[t] = Math.max(n[t], n[t - 1]);
  for (let t = n.length - 2; t >= 0; t -= 1)
    n[t] = Math.min(n[t], n[t + 1]);
  return n.map((t) => x(t));
}
function p(e) {
  return N(A(e.map(C)));
}
function b(e) {
  const n = Array.from(e.querySelectorAll("col")).reduce((r, o) => {
    const s = Number.parseInt(o.getAttribute("span") || "1", 10);
    return r + (Number.isNaN(s) || s < 1 ? 1 : s);
  }, 0), t = Array.from(e.rows).reduce((r, o) => {
    const s = Array.from(o.cells).reduce((u, c) => u + Math.max(c.colSpan || 1, 1), 0);
    return Math.max(r, s);
  }, 0);
  return Math.max(n, t);
}
function z(e) {
  const n = e.domNode.closest("td, th");
  return n instanceof HTMLTableCellElement ? n : null;
}
function v(e, n) {
  const t = Array.from(e.querySelectorAll("col")).map((r) => r.dataset.colId || g());
  return t.length >= n ? t : t.concat(Array.from({
    length: n - t.length
  }, () => g()));
}
function P(e) {
  const n = Array.from(e.rows), t = b(e), r = v(e, t), o = Array.from({
    length: n.length
  }, () => new Array(t).fill(null)), s = /* @__PURE__ */ new Map();
  return n.forEach((u, c) => {
    let l = 0;
    Array.from(u.cells).forEach((a) => {
      for (; l < t && o[c][l]; ) l += 1;
      if (l >= t)
        throw new Error("Invalid table grid: cell exceeds column bounds.");
      const d = w(a.colSpan);
      if (l + d > t)
        throw new Error("Invalid table grid: colspan exceeds column bounds.");
      const i = {
        cell: a,
        colIndex: l,
        colspan: d,
        rowIndex: c,
        rowspan: w(a.rowSpan)
      };
      s.set(a, i);
      const M = Math.min(n.length, c + i.rowspan), y = Math.min(t, l + i.colspan);
      for (let h = c; h < M; h += 1)
        for (let I = l; I < y; I += 1)
          o[h][I] = a;
      l += i.colspan;
    });
  }), {
    colIds: r,
    matrix: o,
    placements: s,
    rows: n,
    table: e
  };
}
function L(e) {
  const n = m(e.table);
  if (!n) return null;
  const t = Array.from({
    length: e.colIds.length + 1
  }, () => []), r = Array.from({
    length: e.rows.length + 1
  }, () => []);
  f(t, 0, n.left), f(t, e.colIds.length, n.right), f(r, 0, n.top), f(r, e.rows.length, n.bottom), e.rows.forEach((c, l) => {
    const a = m(c);
    a && (f(r, l, a.top), f(r, l + 1, a.bottom));
  }), Array.from(e.placements.values()).forEach((c) => {
    const l = m(c.cell);
    l && (f(t, c.colIndex, l.left), f(t, c.colIndex + c.colspan, l.right), f(r, c.rowIndex, l.top), f(r, c.rowIndex + c.rowspan, l.bottom));
  });
  const o = p(t), s = p(r), u = /* @__PURE__ */ new Map();
  return Array.from(e.placements.values()).forEach((c) => {
    const l = o[c.colIndex], a = o[c.colIndex + c.colspan], d = s[c.rowIndex], i = s[c.rowIndex + c.rowspan];
    [l, a, d, i].every(Number.isFinite) && u.set(c.cell, {
      bottom: i,
      left: l,
      right: a,
      top: d
    });
  }), {
    cellRects: u,
    colEdges: o,
    rowEdges: s,
    tableRect: {
      bottom: s.at(-1) ?? n.bottom,
      left: o[0] ?? n.left,
      right: o.at(-1) ?? n.right,
      top: s[0] ?? n.top
    }
  };
}
function q(e) {
  return e.length === 0 ? null : {
    bottom: Math.max(...e.map((n) => n.bottom)),
    left: Math.min(...e.map((n) => n.left)),
    right: Math.max(...e.map((n) => n.right)),
    top: Math.min(...e.map((n) => n.top))
  };
}
function V(e) {
  const n = [], t = /* @__PURE__ */ new Set();
  return e.forEach((r) => {
    !r || t.has(r) || (t.add(r), n.push(r));
  }), n;
}
function E(e) {
  return e.reduce((n, t) => ({
    colEndIndex: Math.max(n.colEndIndex, t.colIndex + t.colspan - 1),
    colStartIndex: Math.min(n.colStartIndex, t.colIndex),
    rowEndIndex: Math.max(n.rowEndIndex, t.rowIndex + t.rowspan - 1),
    rowStartIndex: Math.min(n.rowStartIndex, t.rowIndex)
  }), {
    colEndIndex: -1,
    colStartIndex: Number.POSITIVE_INFINITY,
    rowEndIndex: -1,
    rowStartIndex: Number.POSITIVE_INFINITY
  });
}
function S(e, n, t, r, o) {
  var c;
  const s = [], u = /* @__PURE__ */ new Set();
  for (let l = n; l <= t; l += 1)
    for (let a = r; a <= o; a += 1) {
      const d = (c = e.matrix[l]) == null ? void 0 : c[a];
      if (!d) return null;
      const i = e.placements.get(d);
      if (!i) return null;
      u.has(d) || (u.add(d), s.push(i));
    }
  return s;
}
function R(e, n) {
  let t = n;
  for (; ; ) {
    const r = S(e, t.rowStartIndex, t.rowEndIndex, t.colStartIndex, t.colEndIndex);
    if (!(r != null && r.length)) return null;
    const o = E(r);
    if (!(o.rowStartIndex !== t.rowStartIndex || o.rowEndIndex !== t.rowEndIndex || o.colStartIndex !== t.colStartIndex || o.colEndIndex !== t.colEndIndex)) return o;
    t = o;
  }
}
function T(e, n) {
  const t = S(e, n.rowStartIndex, n.rowEndIndex, n.colStartIndex, n.colEndIndex);
  return t != null && t.length ? t.map((r) => r.cell) : null;
}
function B(e, n) {
  const t = V(n);
  if (t.length === 0) return null;
  const r = t.map((u) => e.placements.get(u)).filter((u) => !!u);
  if (r.length === 0 || r.length !== t.length) return null;
  const o = R(e, E(r));
  if (!o) return null;
  const s = T(e, o);
  return s != null && s.length ? {
    ...o,
    cells: s
  } : null;
}
function D(e, n) {
  var a;
  const t = B(e, n);
  if (!t || t.cells.length <= 1) return null;
  const {
    colEndIndex: r,
    colStartIndex: o,
    rowEndIndex: s,
    rowStartIndex: u
  } = t, c = (a = e.matrix[u]) == null ? void 0 : a[o];
  if (!c) return null;
  const l = e.placements.get(c);
  return (l == null ? void 0 : l.rowIndex) !== u || l.colIndex !== o ? null : {
    baseCell: c,
    cells: t.cells,
    colEndIndex: r,
    colStartIndex: o,
    rowEndIndex: s,
    rowStartIndex: u
  };
}
export {
  D as a,
  P as b,
  b as c,
  B as d,
  L as e,
  q as f,
  z as r
};
