import { jsx as _, jsxs as D } from "react/jsx-runtime";
import { useRef as I, useCallback as R, Children as f, cloneElement as T, isValidElement as B } from "react";
import { Icon as H } from "../icon/index.js";
import '../../assets/index3.css';const P = "_table_1egqd_1", V = "_sortable_1egqd_27", v = {
  table: P,
  sortable: V
}, m = (r) => B(r), h = (r) => f.toArray(r).reduce((e, t) => m(t) ? e + (t.type === "td" || t.type === "th" ? 1 : h(t.props.children)) : e, 0), w = (r) => {
  for (const e of f.toArray(r)) {
    if (!m(e)) continue;
    if (e.type === "tr") return h(e.props.children);
    const t = w(e.props.children);
    if (t !== null) return t;
  }
  return null;
}, E = (r, e) => f.map(r, (t) => {
  if (!m(t)) return t;
  let s = t.props.children === void 0 ? t.props.children : E(t.props.children, e), n;
  return t.type === "td" && t.props["data-span"] === "all" && (n = {
    colSpan: e
  }), t.type === "th" && t.props["data-type"] && (s = /* @__PURE__ */ D("span", { className: v.sortable, children: [
    s,
    /* @__PURE__ */ _(H, { name: "updown", variant: "primary" })
  ] })), n || s !== t.props.children ? T(t, n, s) : t;
}), G = (r, e, t, s, n = 1) => {
  if (t === "text")
    return r.localeCompare(e, s, {
      sensitivity: "base"
    }) * n;
  const l = t === "date" ? Date.parse(r) : Number.parseFloat(r), i = t === "date" ? Date.parse(e) : Number.parseFloat(e);
  return Number.isNaN(l) && Number.isNaN(i) ? 0 : Number.isNaN(l) ? 1 : Number.isNaN(i) ? -1 : (l - i) * n;
}, M = ({
  children: r,
  locale: e,
  ...t
}) => {
  const s = I(null), n = w(r), l = n && n > 0 ? E(r, n) : r, i = (u, c, o) => {
    u.querySelectorAll("th[data-type]").forEach((a) => {
      a === c ? (a.dataset.dir = o, a.setAttribute("aria-sort", o === "asc" ? "ascending" : "descending")) : (delete a.dataset.dir, a.setAttribute("aria-sort", "none"));
    });
  }, S = R((u) => {
    var C, x;
    const c = s.current;
    if (!c) return;
    const o = (C = u.target) == null ? void 0 : C.closest("th");
    if (!o) return;
    const p = c.tHead;
    if (!p || !p.contains(o)) return;
    const a = o.cellIndex;
    if (a < 0) return;
    const d = (x = c.tBodies) == null ? void 0 : x[0];
    if (!d) return;
    const b = o.getAttribute("data-type");
    if (!b) return;
    const N = o.dataset.dir === "asc" ? "desc" : "asc";
    i(p, o, N);
    const q = N === "asc" ? 1 : -1, y = Array.from(d.rows);
    y.sort((F, j) => {
      var g, A;
      const k = (((g = F.cells[a]) == null ? void 0 : g.textContent) ?? "").trim(), z = (((A = j.cells[a]) == null ? void 0 : A.textContent) ?? "").trim();
      return G(k, z, b, e, q);
    }), d.append(...y);
  }, [e]);
  return /* @__PURE__ */ _("table", { className: v.table, onClick: S, ref: s, ...t, "data-component": "table", children: l });
};
export {
  M as Table
};
//# sourceMappingURL=index.js.map
