function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import React from "react";
import styled from "styled-components";
import { flexRender } from "@tanstack/react-table";
import Flex from "../../../templates/flex";
import { Text } from "../../../typography";
import { useTableState } from "../../provider";
import ResizeHandler from "./resizeHandler";
import Sorting, { SortIconContainer } from "./sorting";
import Info from "./info";
import Filter from "./filter";
import useSortableHeader from "./sortableHeader";
import DragHandle from "./dragHandle";
var Label = styled(Text).withConfig({
  displayName: "cell__Label",
  componentId: "sc-z651qs-0"
})(["width:100%;*{white-space:nowrap;text-overflow:ellipsis;overflow:hidden;}"]);
var LabelContainer = styled(Flex).withConfig({
  displayName: "cell__LabelContainer",
  componentId: "sc-z651qs-1"
})(["&:hover ", "{opacity:1;}"], SortIconContainer);
var rerenderSelector = function rerenderSelector(state) {
  return {
    sorting: state.sorting,
    sizing: state.columnSizing,
    selecting: state.selectedRows
  };
};
var BodyHeaderCell = function BodyHeaderCell(_ref) {
  var header = _ref.header,
    table = _ref.table,
    testPrefix = _ref.testPrefix,
    coloredSortedColumn = _ref.coloredSortedColumn,
    index = _ref.index,
    children = _ref.children,
    isSubheader = _ref.isSubheader,
    hasSubheaders = _ref.hasSubheaders,
    enableColumnReordering = _ref.enableColumnReordering;
  useTableState(rerenderSelector);
  var column = header.column;
  var _useSortableHeader = useSortableHeader(column.id, enableColumnReordering),
    sortableRef = _useSortableHeader.sortableRef,
    sortableStyle = _useSortableHeader.sortableStyle,
    dragHandleProps = _useSortableHeader.dragHandleProps,
    isDragging = _useSortableHeader.isDragging;
  var tableMeta = typeof column.columnDef.tableMeta === "function" ? column.columnDef.tableMeta({}, column, index) : column.columnDef.tableMeta;
  var meta = typeof column.columnDef.meta === "function" ? column.columnDef.meta({}, column, index) : column.columnDef.meta;
  var headStyles = _extends({}, (tableMeta == null ? void 0 : tableMeta.styles) || {}, (meta == null ? void 0 : meta.styles) || {}, (tableMeta == null ? void 0 : tableMeta.headStyles) || {}, (meta == null ? void 0 : meta.headStyles) || {});
  return /*#__PURE__*/React.createElement(Flex, _extends({
    ref: sortableRef,
    style: enableColumnReordering ? sortableStyle : undefined,
    flex: !column.columnDef.fullWidth && (column.columnDef.notFlex || column.getCanResize()) ? false : header.colSpan,
    width: (header.subHeaders.length ? header.subHeaders.reduce(function (s, h) {
      return s + h.column.getSize();
    }, 0) : column.getSize()) + "px",
    height: {
      min: "45px"
    },
    position: "relative"
  }, column.getCanSort() && coloredSortedColumn && !!column.getIsSorted() && {
    background: "columnHighlight",
    backgroundOpacity: "0.2"
  }, {
    padding: !hasSubheaders || isSubheader ? [1, 2] : [0]
  }, headStyles, {
    column: true
  }), /*#__PURE__*/React.createElement(Flex, {
    flex: true,
    column: true,
    width: "100%",
    alignItems: column.columnDef.align || "start",
    justifyContent: "center",
    gap: 1
  }, /*#__PURE__*/React.createElement(LabelContainer, {
    "data-testid": "netdata-table-header-cell-label-container",
    alignItems: "center",
    cursor: column.getCanSort() ? "pointer" : "default",
    onClick: column.getCanSort() ? column.getToggleSortingHandler() : undefined,
    padding: [0, 0, 0, !hasSubheaders || isSubheader ? 0 : 2],
    overflow: "hidden",
    width: "100%"
  }, /*#__PURE__*/React.createElement(DragHandle, {
    dragHandleProps: dragHandleProps,
    visible: isDragging
  }), column.isPlaceholder ? null : /*#__PURE__*/React.createElement(Label, _extends({
    as: column.columnDef.labelAs
  }, column.columnDef.labelProps, {
    sorting: column.getIsSorted(),
    sortable: column.getCanSort(),
    strong: true
  }), flexRender(column.columnDef.header, header.getContext())), /*#__PURE__*/React.createElement(Sorting, {
    sortable: column.getCanSort(),
    sorting: column.getIsSorted()
  }), /*#__PURE__*/React.createElement(Info, {
    meta: meta
  })), /*#__PURE__*/React.createElement(Filter, {
    column: column,
    testPrefix: testPrefix,
    index: index
  })), /*#__PURE__*/React.createElement(ResizeHandler, {
    header: header,
    table: table
  }), children);
};
export default BodyHeaderCell;