import { useState as f, useMemo as l } from "react";
import { SortDirection as t, naturalSortByProperty as m } from "../utils/SortUtils.js";
const K = (c, I, d = t.ASCENDING, D = !0, N = !1) => {
  const [o, g] = f(I), [e, a] = f(d);
  return { sortedItems: l(() => {
    const s = (n, i) => D ? m(n, i, e, N) : n.sort((p, y) => {
      const r = p[i], u = y[i];
      return r < u ? e === t.ASCENDING ? -1 : 1 : r > u ? e === t.ASCENDING ? 1 : -1 : 0;
    });
    if (Array.isArray(o)) {
      const [n, i] = o, y = s(c, n).reduce(
        (r, u) => {
          const S = String(u[n]);
          return r[S] || (r[S] = []), r[S].push(u), r;
        },
        {}
      );
      return Object.values(y).flatMap((r) => s(r, i));
    }
    return s(c, o);
  }, [c, o, e, D, N]), sortKey: o, sortDirection: e, setSortKey: g, setSortDirection: a, toggleDirection: () => {
    a(
      (s) => s === t.ASCENDING ? t.DESCENDING : t.ASCENDING
    );
  } };
};
export {
  K as default
};
//# sourceMappingURL=useSorting.js.map
