import { useState as D, useEffect as v } from "react";
import h from "../../../utils/arrayMove.js";
const K = (s, t) => {
  const l = t.map((n) => n.key), i = new Set(l), u = s.filter((n) => i.has(n)), a = l.filter((n) => !u.includes(n));
  return [...u, ...a];
}, O = (s) => s.draggable === !0, E = (s) => {
  const { columns: t, columnOrder: l, enabled: i, onColumnOrderChange: u } = s, [a, n] = D(() => t.map((e) => e.key)), p = K(l ?? a, t), d = l !== void 0;
  v(() => {
    d || n((e) => {
      const r = K(e, t);
      return JSON.stringify(e) === JSON.stringify(r) ? e : r;
    });
  }, [t, d]);
  const S = new Map(t.map((e) => [e.key, e])), m = p.flatMap((e) => {
    const r = S.get(e);
    return r ? [r] : [];
  }), c = m.filter(O).map((e) => e.key);
  return {
    draggableColumnKeys: c,
    handleColumnDragEnd: (e) => {
      if (!i)
        return;
      const r = String(e.active.id), y = e.over ? String(e.over.id) : void 0;
      if (!y || r === y)
        return;
      const C = c.indexOf(r), f = c.indexOf(y);
      if (C < 0 || f < 0)
        return;
      const b = h(c, C, f);
      let x = 0;
      const g = m.map((o) => {
        if (!O(o))
          return o.key;
        const k = b[x];
        return x += 1, k ?? o.key;
      });
      d || n(
        (o) => JSON.stringify(o) === JSON.stringify(g) ? o : g
      ), u?.(g);
    },
    orderedColumns: m
  };
};
export {
  E as useDraggableColumns
};
//# sourceMappingURL=useDraggableColumns.js.map
