"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _flex = _interopRequireDefault(require("../../../templates/flex"));
var _provider = require("../../provider");
var _cell = _interopRequireDefault(require("./cell"));
var _excluded = ["id", "headers", "testPrefix", "rowReverse"],
  _excluded2 = ["groups", "size", "side", "flex"],
  _excluded3 = ["table", "testPrefix"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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 _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 _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
var rerenderSelector = function rerenderSelector(state) {
  return {
    sizing: state.columnSizing,
    expanded: state.expanded,
    columnVisibility: state.columnVisibility,
    selectedRows: state.selectedRows,
    grouping: state.grouping,
    columnsCount: state.table && state.table.getAllColumns().length
  };
};
var HeaderGroup = function HeaderGroup(_ref) {
  var id = _ref.id,
    headers = _ref.headers,
    testPrefix = _ref.testPrefix,
    rowReverse = _ref.rowReverse,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  return /*#__PURE__*/_react["default"].createElement(_flex["default"], {
    id: id,
    "data-testid": "netdata-table-headRow" + testPrefix,
    flex: true,
    background: "tableRowBg2",
    rowReverse: rowReverse
  }, headers.map(function (header, index) {
    return /*#__PURE__*/_react["default"].createElement(_cell["default"], _extends({
      key: header.id,
      index: index
    }, rest, {
      header: header,
      testPrefix: testPrefix,
      hasSubheaders: !!header.subHeaders.length
    }), !!header.subHeaders.length && /*#__PURE__*/_react["default"].createElement(HeaderGroup, _extends({
      headers: header.subHeaders,
      id: header.id,
      key: header.id
    }, rest, {
      isSubheader: true
    })));
  }));
};
var HeaderGroups = function HeaderGroups(_ref2) {
  var groups = _ref2.groups,
    size = _ref2.size,
    side = _ref2.side,
    _ref2$flex = _ref2.flex,
    flex = _ref2$flex === void 0 ? "grow" : _ref2$flex,
    rest = _objectWithoutPropertiesLoose(_ref2, _excluded2);
  if (!groups[0].headers.length) return null;
  return /*#__PURE__*/_react["default"].createElement(_flex["default"], _extends({
    position: side ? "sticky" : "relative"
  }, side === "right" ? {
    right: 0,
    border: {
      side: "left"
    }
  } : {
    left: 0,
    border: {
      side: "right"
    }
  }, {
    zIndex: side ? 11 : undefined,
    width: size + "px",
    flex: flex,
    column: true
  }), /*#__PURE__*/_react["default"].createElement(HeaderGroup, _extends({
    headers: groups[0].headers,
    id: groups[0].id,
    key: groups[0].id
  }, rest)));
};
var BodyHeader = /*#__PURE__*/(0, _react.memo)(function (_ref3) {
  var table = _ref3.table,
    testPrefix = _ref3.testPrefix,
    rest = _objectWithoutPropertiesLoose(_ref3, _excluded3);
  (0, _provider.useTableState)(rerenderSelector);
  return /*#__PURE__*/_react["default"].createElement(_flex["default"], {
    "data-testid": "netdata-table-head" + testPrefix,
    flex: true,
    border: {
      size: "1px",
      type: "solid",
      side: "bottom",
      color: "border"
    }
  }, /*#__PURE__*/_react["default"].createElement(HeaderGroups, _extends({
    groups: table.getLeftHeaderGroups(),
    side: "left",
    size: table.getLeftTotalSize(),
    testPrefix: testPrefix
  }, rest, {
    flex: false,
    table: table
  })), /*#__PURE__*/_react["default"].createElement(HeaderGroups, _extends({
    groups: table.getCenterHeaderGroups(),
    size: table.getCenterTotalSize(),
    testPrefix: testPrefix
  }, rest, {
    table: table
  })), /*#__PURE__*/_react["default"].createElement(HeaderGroups, _extends({
    groups: table.getRightHeaderGroups(),
    side: "right",
    size: table.getRightTotalSize(),
    testPrefix: testPrefix
  }, rest, {
    flex: false,
    table: table,
    rowReverse: true
  })));
});
var _default = exports["default"] = BodyHeader;