import React__default, { useContext, useMemo } from "react";
import Checkbox from "../../Checkbox/checkbox.js";
import Column from "./column.js";
import cs from "../../_util/classNames.js";
import useComponent from "../hooks/useComponent.js";
import { INTERNAL_SELECTION_KEY, INTERNAL_EXPAND_KEY } from "../constant.js";
import { ConfigContext } from "../../ConfigProvider/index.js";
import { jsx, jsxs } from "../../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
function THead(props) {
  var sorter = props.sorter, expandedRowRender = props.expandedRowRender, _a = props.expandProps, expandProps = _a === void 0 ? {} : _a, onSort = props.onSort, onHandleFilter = props.onHandleFilter, onHandleFilterReset = props.onHandleFilterReset, onHeaderRow = props.onHeaderRow, prefixCls = props.prefixCls, currentFilters = props.currentFilters, components = props.components, data = props.data, selectedRowKeys = props.selectedRowKeys, rowSelection = props.rowSelection, _b = props.allSelectedRowKeys, allSelectedRowKeys = _b === void 0 ? [] : _b, groupColumns = props.groupColumns, stickyOffsets = props.stickyOffsets, groupStickyClassNames = props.groupStickyClassNames, showSorterTooltip = props.showSorterTooltip;
  var rtl = useContext(ConfigContext).rtl;
  var _c = useComponent(components), ComponentThead = _c.ComponentThead, ComponentHeaderRow = _c.ComponentHeaderRow, getHeaderComponentOperations = _c.getHeaderComponentOperations;
  var _checkbox = rowSelection && (rowSelection.type === "checkbox" || !("type" in rowSelection));
  var _checkAll = rowSelection && "checkAll" in rowSelection ? rowSelection.checkAll : true;
  var isRadio = rowSelection && rowSelection.type === "radio";
  var expandColumnTitle = expandProps.columnTitle;
  var currentSelectedRowKeys = useMemo(function() {
    var tempSet = new Set(allSelectedRowKeys);
    return selectedRowKeys.filter(function(v) {
      return tempSet.has(v);
    });
  }, [selectedRowKeys, allSelectedRowKeys]);
  var selectionRowSpanProps = groupColumns.length > 1 ? {
    rowSpan: groupColumns.length
  } : {};
  var operationClassName = cs(prefixCls + "-th", prefixCls + "-operation");
  return /* @__PURE__ */ jsx(ComponentThead, {
    children: groupColumns.map(function(row, index) {
      var headerRowProps = onHeaderRow && onHeaderRow(row, index);
      var selectionNode = (_checkbox || isRadio) && index === 0 && /* @__PURE__ */ jsx("th", {
        className: cs(operationClassName, prefixCls + "-" + (isRadio ? "radio" : "checkbox")),
        children: /* @__PURE__ */ jsxs("div", {
          className: prefixCls + "-th-item",
          children: [_checkAll && !isRadio ? /* @__PURE__ */ jsx(Checkbox, {
            indeterminate: data && currentSelectedRowKeys.length > 0 && currentSelectedRowKeys.length !== allSelectedRowKeys.length,
            checked: data && currentSelectedRowKeys.length !== 0 && currentSelectedRowKeys.length === allSelectedRowKeys.length,
            disabled: !allSelectedRowKeys.length,
            onChange: props.onCheckAll
          }) : null, rowSelection && rowSelection.columnTitle]
        })
      });
      var expandNode = expandedRowRender && /* @__PURE__ */ jsx("th", {
        className: cs(operationClassName, prefixCls + "-expand"),
        children: expandColumnTitle && /* @__PURE__ */ jsx("div", {
          className: prefixCls + "-th-item",
          children: expandColumnTitle
        })
      });
      var stickyClassNames = groupStickyClassNames[index];
      var headerOperations = getHeaderComponentOperations({
        selectionNode,
        expandNode
      });
      return /* @__PURE__ */ jsx(ComponentHeaderRow, {
        ...__assign({}, headerRowProps, {
          key: index,
          className: prefixCls + "-tr"
        }),
        children: row.map(function(column, colIndex) {
          var _a2;
          var _b2, _c2, _d, _e;
          var stickyOffset = stickyOffsets[colIndex];
          var stickyClassName = stickyClassNames[colIndex];
          if (column.$$isOperation) {
            var node = column.node;
            var isExtraOperation = true;
            if (column.title === INTERNAL_SELECTION_KEY) {
              node = (_b2 = headerOperations.find(function(o) {
                return o.name === "selectionNode";
              })) === null || _b2 === void 0 ? void 0 : _b2.node;
              isExtraOperation = false;
            }
            if (column.title === INTERNAL_EXPAND_KEY) {
              node = (_c2 = headerOperations.find(function(o) {
                return o.name === "expandNode";
              })) === null || _c2 === void 0 ? void 0 : _c2.node;
              isExtraOperation = false;
            }
            var operationNode = node;
            return React__default.cloneElement(operationNode, __assign(__assign(__assign({
              key: column.key || colIndex
            }, operationNode.props), selectionRowSpanProps), {
              className: cs(isExtraOperation ? operationClassName : "", (_d = operationNode === null || operationNode === void 0 ? void 0 : operationNode.props) === null || _d === void 0 ? void 0 : _d.className, stickyClassName),
              style: __assign(__assign(__assign({}, (_e = operationNode === null || operationNode === void 0 ? void 0 : operationNode.props) === null || _e === void 0 ? void 0 : _e.style), column.fixed === "left" ? (_a2 = {}, _a2[rtl ? "right" : "left"] = stickyOffset, _a2) : {}), {
                width: column.width,
                minWidth: column.width
              })
            }));
          }
          var headerCellProps = column.onHeaderCell && column.onHeaderCell(column, colIndex);
          var columnClassName = cs(stickyClassName, column.className);
          var columnFixedStyle = {};
          if (column.fixed === "left") {
            columnFixedStyle[rtl ? "right" : "left"] = stickyOffset;
          }
          if (column.fixed === "right") {
            columnFixedStyle[rtl ? "left" : "right"] = stickyOffset;
          }
          return /* @__PURE__ */ jsx(Column, {
            ...__assign({
              key: column.key,
              index: colIndex,
              onSort,
              onHandleFilter,
              onHandleFilterReset,
              currentSorter: sorter,
              currentFilters,
              _key: column.key || column.dataIndex || colIndex
            }, column, {
              column,
              headerCellProps,
              prefixCls,
              components,
              className: columnClassName,
              columnFixedStyle,
              showSorterTooltip
            })
          });
        })
      });
    })
  });
}
export {
  THead as default
};
//# sourceMappingURL=index.js.map
