import { useMemo as r, useState as d, useEffect as x } from "react";
import { resolveRowKey as m } from "../model/resolveRowMeta.js";
const j = (u) => {
  const { rows: o, rowKey: a, initialActiveRowId: w, initialSelectedRowIds: R = [], isRowSelectable: f } = u, h = r(() => o.map((e, t) => m(e, t, a)), [o, a]), S = r(() => new Set(h), [h]), n = r(
    () => o.flatMap((e, t) => {
      const l = m(e, t, a);
      return f?.(e, t) === !1 ? [] : [l];
    }),
    [f, a, o]
  ), i = r(() => new Set(n), [n]), [y, g] = d(w), [s, c] = d(R);
  x(() => {
    c((e) => {
      const t = e.filter(
        (l) => S.has(l) && i.has(l)
      );
      return t.length === e.length && t.every((l, M) => l === e[M]) ? e : t;
    }), g(
      (e) => e !== void 0 && S.has(e) ? e : void 0
    );
  }, [S, i]);
  const b = (e) => {
    i.has(e) && c(
      (t) => t.includes(e) ? t.filter((l) => l !== e) : [...t, e]
    );
  }, A = (e = !n.every((t) => s.includes(t))) => {
    c(e ? n : []);
  }, K = () => {
    c([]);
  };
  return {
    activeRowId: y,
    selectedRowIds: s,
    hasSelection: s.length > 0,
    isAllSelected: n.length > 0 && n.every((e) => s.includes(e)),
    isRowSelected: (e) => i.has(e) && s.includes(e),
    setActiveRowId: g,
    setSelectedRowIds: c,
    toggleRowSelection: b,
    toggleAllSelection: A,
    clearSelection: K
  };
};
export {
  j as default
};
//# sourceMappingURL=useTableSelection.js.map
