"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"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
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); }
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); }
var Label = (0, _styledComponents["default"])(_typography.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 = (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(_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;
  (0, _provider.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["default"].createElement(_flex["default"], _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["default"].createElement(_flex["default"], {
    flex: true,
    column: true,
    width: "100%",
    alignItems: column.columnDef.align || "start",
    gap: 1
  }, /*#__PURE__*/_react["default"].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["default"].createElement(_sorting["default"], {
    sortable: column.getCanSort(),
    sorting: column.getIsSorted()
  }), column.isPlaceholder ? null : /*#__PURE__*/_react["default"].createElement(Label, _extends({
    as: column.columnDef.labelAs
  }, column.columnDef.labelProps, {
    sorting: column.getIsSorted(),
    sortable: column.getCanSort()
  }), (0, _reactTable.flexRender)(column.columnDef.header, header.getContext()))), /*#__PURE__*/_react["default"].createElement(_filter["default"], {
    column: column,
    testPrefix: testPrefix,
    index: index
  })), /*#__PURE__*/_react["default"].createElement(_info["default"], {
    meta: meta
  }), /*#__PURE__*/_react["default"].createElement(_resizeHandler["default"], {
    header: header,
    table: table
  }), children);
};
var _default = exports["default"] = BodyHeaderCell;