import l, { useState as y, useEffect as R, useMemo as i, useCallback as N } from "react";
import V from "../utils/convertToColumnDef.js";
import { SELECTION_COLUMN_SIZE as E, SELECTION_COLUMN_ID as Z, ACTION_COLUMN_SIZE as B, ACTION_COLUMN_ID as L } from "../constants/tableDefaults.js";
import M from "../../checkbox/Checkbox.js";
import H from "../../jtl-dropdown/JTLDropdown.js";
import J from "../../button/Button.js";
function X(m, C, u, t, n) {
  const [T, O] = y(m), [z, _] = y(() => m.map((e) => e.key)), D = ((n == null ? void 0 : n.value) ?? []).join(",");
  R(() => {
    var e;
    (e = n == null ? void 0 : n.value) != null && e.length && _(n.value);
  }, [D]);
  const d = ((t == null ? void 0 : t.value) ?? []).map((e, s) => String(e.key ?? s)).join(",");
  R(() => {
    var e;
    (e = t == null ? void 0 : t.value) != null && e.length && O(t.value);
  }, [d]);
  const p = i(
    () => {
      var e;
      return n != null && n.enabled ? (e = n == null ? void 0 : n.value) != null && e.length ? n.value : z : [];
    },
    [n, z]
  ), K = N(
    (e) => {
      const s = typeof e == "function" ? e(p) : e;
      n != null && n.onChange && n.onChange(s), _(s);
    },
    [p, n]
  ), v = t && t.value ? t.value : T, a = i(
    () => ({
      id: Z,
      header: ({ table: e }) => l.createElement(M, {
        "aria-label": "Select all rows",
        value: e.getIsAllRowsSelected(),
        indeterminate: !e.getIsAllRowsSelected() && e.getIsSomeRowsSelected(),
        onChange: (s) => e.toggleAllRowsSelected(!!s)
      }),
      cell: ({ row: e }) => l.createElement(M, {
        "aria-label": "Select row",
        value: e.getIsSelected(),
        onChange: (s) => e.toggleSelected(!!s),
        onClick: (s) => s.stopPropagation(),
        // Radix Checkbox follows WAI-ARIA spec and only responds to Space, not Enter.
        // We add Enter support here so keyboard users can toggle selection without
        // having to know that only Space works — Enter is the expected activation
        // key for most interactive elements.
        onKeyDown: (s) => {
          s.key === "Enter" && e.toggleSelected(!e.getIsSelected());
        }
      }),
      enableSorting: !1,
      enableResizing: !1,
      minSize: E,
      maxSize: E,
      size: E
    }),
    []
  ), f = i(() => {
    if (!(!(u != null && u.enabled) || !(u != null && u.menuItems) || (u == null ? void 0 : u.menuItems.length) === 0))
      return {
        accessorKey: L,
        id: L,
        header: "",
        cell: ({ row: e }) => {
          var I;
          const s = (I = u == null ? void 0 : u.menuItems) == null ? void 0 : I.map((h) => ({
            ...h,
            onClick: () => {
              var k;
              (k = h == null ? void 0 : h.onClick) == null || k.call(h, e.original);
            }
          }));
          return l.createElement(
            H,
            {
              position: "right",
              menuItems: s || []
            },
            l.createElement(J, {
              "aria-label": "Row action button",
              variant: "ghost",
              icon: "MoreHorizontal",
              size: "icon"
            })
          );
        },
        size: B
      };
  }, [u]), U = i(() => {
    const e = V(v), I = (C == null ? void 0 : C.enabled) && ((C == null ? void 0 : C.menuItems) || []).length > 0 ? [a, ...e] : e;
    return f != null && f.id && I.push(f), I;
  }, [v, C, a, f]), x = i(() => {
    const e = {};
    return m.forEach((s) => {
      e[s.key] = s.width || 150;
    }), e;
  }, [m]), j = N(
    (e) => {
      t != null && t.onChange && t.onChange(e), O(e);
    },
    [t]
  );
  return {
    memoizedColumns: U,
    selectionColumn: a,
    actionColumn: f,
    columnInitialSize: x,
    selectedColumns: v,
    columnOrder: p,
    handleSelectedColumns: j,
    setColumnOrder: K
  };
}
export {
  X as default
};
//# sourceMappingURL=useTableColumns.js.map
