"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _reactTable = require("@tanstack/react-table");
var _flex = _interopRequireDefault(require("../../../templates/flex"));
var _typography = require("../../../typography");
var _provider = require("../../provider");
var _resizeHandler = _interopRequireDefault(require("./resizeHandler"));
var _sorting = _interopRequireWildcard(require("./sorting"));
var _info = _interopRequireDefault(require("./info"));
var _filter = _interopRequireDefault(require("./filter"));
var _sortableHeader = _interopRequireDefault(require("./sortableHeader"));
var _dragHandle = _interopRequireDefault(require("./dragHandle"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
var Label = (0, _styledComponents["default"])(_typography.Text).withConfig({
  displayName: "cell__Label",
  componentId: "sc-z651qs-0"
})(["width:100%;*{white-space:nowrap;text-overflow:ellipsis;overflow:hidden;}"]);
var LabelContainer = (0, _styledComponents["default"])(_flex["default"]).withConfig({
  displayName: "cell__LabelContainer",
  componentId: "sc-z651qs-1"
})(["&:hover ", "{opacity:1;}"], _sorting.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;
  (0, _provider.useTableState)(rerenderSelector);
  var column = header.column;
  var _useSortableHeader = (0, _sortableHeader["default"])(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["default"].createElement(_flex["default"], _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["default"].createElement(_flex["default"], {
    flex: true,
    column: true,
    width: "100%",
    alignItems: column.columnDef.align || "start",
    justifyContent: "center",
    gap: 1
  }, /*#__PURE__*/_react["default"].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["default"].createElement(_dragHandle["default"], {
    dragHandleProps: dragHandleProps,
    visible: isDragging
  }), column.isPlaceholder ? null : /*#__PURE__*/_react["default"].createElement(Label, _extends({
    as: column.columnDef.labelAs
  }, column.columnDef.labelProps, {
    sorting: column.getIsSorted(),
    sortable: column.getCanSort(),
    strong: true
  }), (0, _reactTable.flexRender)(column.columnDef.header, header.getContext())), /*#__PURE__*/_react["default"].createElement(_sorting["default"], {
    sortable: column.getCanSort(),
    sorting: column.getIsSorted()
  }), /*#__PURE__*/_react["default"].createElement(_info["default"], {
    meta: meta
  })), /*#__PURE__*/_react["default"].createElement(_filter["default"], {
    column: column,
    testPrefix: testPrefix,
    index: index
  })), /*#__PURE__*/_react["default"].createElement(_resizeHandler["default"], {
    header: header,
    table: table
  }), children);
};
var _default = exports["default"] = BodyHeaderCell;