import { jsxs as S, jsx as o } from "react/jsx-runtime";
import { useSensors as N, useSensor as V, PointerSensor as j, DndContext as B, DragOverlay as M } from "@dnd-kit/core";
import { restrictToHorizontalAxis as W } from "@dnd-kit/modifiers";
import { SortableContext as $, horizontalListSortingStrategy as G } from "@dnd-kit/sortable";
import { useRef as Y, useState as b, useLayoutEffect as q } from "react";
import F from "../../hooks/useMergeRefs.js";
import J from "../../hooks/useResizeObserver.js";
import y from "../../utils/classNames.js";
import Q from "./render/header/TableHeaderDragOverlay.js";
import U from "./render/header/TableHeaderSelectionCell.js";
import { resolveColumnCollisionDetection as X } from "./render/header/resolveColumnCollisionDetection.js";
import { useOptionalTableInteractionContext as Z } from "./context/TableInteractionContext.js";
import { useOptionalTableLayoutContext as ee } from "./context/TableLayoutContext.js";
import { useOptionalTableRenderConfigContext as te } from "./context/TableRenderConfigContext.js";
import { TableSectionContext as R, TableHeaderContext as w } from "./context/TableStructureContext.js";
import { useOptionalTableRenderContext as oe } from "./context/TableRenderContext.js";
const m = "--table-sticky-header-height", xe = (D) => {
  const { children: u, className: H, sticky: i = !1, ...T } = D, s = oe(), c = te(), t = Z(), n = ee();
  if (!s || !c || !t || !n)
    return null;
  const A = N(V(j, { activationConstraint: { distance: 6 } })), r = Y(0), [g, , { blockSize: C }] = J();
  r.current = 0;
  const [h, f] = b(void 0), [E, v] = b(void 0), P = F(
    n.tableHeadRowRef,
    g
  ), k = !!n.onColumnDragEnd && (n.draggableColumnKeys?.length ?? 0) > 1, d = s.header, l = d?.rowCount ?? 1, I = d ?? {
    rowCount: l,
    totalColumnCount: s.columns.length,
    columns: []
  }, K = h ? d?.columns.find((e) => (e.columnKey ?? String(e.key)) === h) : void 0, p = () => {
    f(void 0), v(void 0);
  }, O = (e) => {
    f(String(e.active.id)), v(e.active.rect.current.initial?.width ?? void 0);
  }, z = (e) => {
    p();
  }, _ = (e) => {
    p(), n.onColumnDragEnd?.(e);
  };
  q(() => {
    const e = g.current, a = e?.closest(".table");
    if (!a)
      return;
    if (!i || !e) {
      a.style.removeProperty(m);
      return;
    }
    const L = Math.max(C ?? e.getBoundingClientRect().height ?? 0, 0);
    return a.style.setProperty(m, `${L}px`), () => {
      a.style.removeProperty(m);
    };
  }, [C, i]);
  const x = k && n.draggableColumnKeys ? /* @__PURE__ */ S(
    B,
    {
      sensors: A,
      collisionDetection: X,
      modifiers: [W],
      onDragStart: O,
      onDragCancel: z,
      onDragEnd: _,
      children: [
        /* @__PURE__ */ o($, { items: n.draggableColumnKeys, strategy: G, children: u }),
        /* @__PURE__ */ o(M, { adjustScale: !1, dropAnimation: null, children: /* @__PURE__ */ o(
          Q,
          {
            column: K,
            header: I,
            showSelectionColumn: t.showSelectionColumn,
            width: E
          }
        ) })
      ]
    }
  ) : u;
  return c.isTableView ? /* @__PURE__ */ o(
    "div",
    {
      ...T,
      className: y("table-head", i && "table-head-sticky"),
      role: "rowgroup",
      ref: P,
      children: /* @__PURE__ */ o(R.Provider, { value: "header", children: /* @__PURE__ */ o(
        w.Provider,
        {
          value: {
            nextHeaderRowIndex: () => {
              const e = r.current;
              return r.current += 1, e;
            }
          },
          children: /* @__PURE__ */ S(
            "div",
            {
              className: y(
                "table-head-grid",
                t.selectionHeaderContent && "table-head-grid-custom-selection",
                H
              ),
              role: l === 1 ? "row" : "presentation",
              style: { gridTemplateRows: `repeat(${l}, auto)` },
              children: [
                t.showSelectionColumn && /* @__PURE__ */ o(
                  U,
                  {
                    isAllSelected: t.isAllRowsSelected,
                    isSomeRowsSelected: t.isSomeRowsSelected,
                    onToggleAllSelection: t.onToggleAllSelection,
                    disabled: !t.hasSelectableRows,
                    rowCount: l,
                    selectionHeaderContent: t.selectionHeaderContent,
                    selectionCheckboxVerticalAlignment: c.selectionCheckboxVerticalAlignment
                  }
                ),
                x
              ]
            }
          )
        }
      ) })
    }
  ) : /* @__PURE__ */ o(R.Provider, { value: "header", children: /* @__PURE__ */ o(
    w.Provider,
    {
      value: {
        nextHeaderRowIndex: () => {
          const e = r.current;
          return r.current += 1, e;
        }
      },
      children: x
    }
  ) });
};
export {
  xe as default
};
//# sourceMappingURL=TableHeader.js.map
