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); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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";
var Label = styled(Text).withConfig({
  displayName: "cell__Label",
  componentId: "sc-z651qs-0"
})(["width:100%;transition:transform 200ms ease;*{white-space:nowrap;text-overflow:ellipsis;overflow:hidden;}", " ", ""], function (_ref) {
  var sorting = _ref.sorting;
  return sorting && "transform: translateX(12px);";
}, function (_ref2) {
  var sortable = _ref2.sortable;
  return sortable && "\n    &:hover {\n      transform: translateX(12px);\n    }\n  ";
});
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(_ref3) {
  var header = _ref3.header,
    table = _ref3.table,
    testPrefix = _ref3.testPrefix,
    coloredSortedColumn = _ref3.coloredSortedColumn,
    index = _ref3.index,
    children = _ref3.children,
    isSubheader = _ref3.isSubheader,
    hasSubheaders = _ref3.hasSubheaders;
  useTableState(rerenderSelector);
  var column = header.column;
  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 = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, (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({
    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",
    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",
    gap: 1
  }, /*#__PURE__*/React.createElement(LabelContainer, {
    alignItems: "center",
    cursor: column.getCanSort() ? "pointer" : "default",
    onClick: column.getCanSort() ? column.getToggleSortingHandler() : undefined,
    padding: [0, 2, 0, !hasSubheaders || isSubheader ? 0 : 2],
    overflow: "hidden",
    width: "100%"
  }, /*#__PURE__*/React.createElement(Sorting, {
    sortable: column.getCanSort(),
    sorting: column.getIsSorted()
  }), column.isPlaceholder ? null : /*#__PURE__*/React.createElement(Label, _extends({
    as: column.columnDef.labelAs
  }, column.columnDef.labelProps, {
    sorting: column.getIsSorted(),
    sortable: column.getCanSort()
  }), flexRender(column.columnDef.header, header.getContext()))), /*#__PURE__*/React.createElement(Filter, {
    column: column,
    testPrefix: testPrefix,
    index: index
  })), /*#__PURE__*/React.createElement(Info, {
    meta: meta
  }), /*#__PURE__*/React.createElement(ResizeHandler, {
    header: header,
    table: table
  }), children);
};
export default BodyHeaderCell;