"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _mobx = require("mobx");

var _mobxReactLite = require("mobx-react-lite");

var _UnitConvertor = require("../../../lib/_util/UnitConvertor");

var _ResizeObservedRow = _interopRequireDefault(require("./ResizeObservedRow"));

var _TableContext = _interopRequireDefault(require("./TableContext"));

var _utils = require("./utils");

var TableHeaderRow = function TableHeaderRow(props) {
  var rowIndex = props.rowIndex,
      lock = props.lock,
      children = props.children,
      rows = props.rows;

  var _useContext = (0, _react.useContext)(_TableContext["default"]),
      rowHeight = _useContext.rowHeight,
      tableStore = _useContext.tableStore;

  var setRowHeight = (0, _react.useCallback)((0, _mobx.action)(function (index, height) {
    (0, _mobx.set)(tableStore.lockColumnsHeadRowsHeight, index, height);
  }), [tableStore]);

  var getRowHeight = function getRowHeight(index) {
    return (0, _mobx.get)(tableStore.lockColumnsHeadRowsHeight, index) || 0;
  };

  var getHeaderRowStyle = function getHeaderRowStyle() {
    var height = getRowHeight(rowIndex);
    return (0, _UnitConvertor.pxToRem)(rows.slice(rowIndex + 1).reduce(function (total, r, index) {
      return r.length ? total : total + getRowHeight(index + rowIndex + 1);
    }, height), true);
  };

  var needStoreRowHeight = !(0, _utils.isStickySupport)() && (tableStore.headerRowHeight === 'auto' || rowHeight === 'auto' || rows.length > 1);
  var style = lock && needStoreRowHeight ? {
    height: getHeaderRowStyle()
  } : undefined;

  var tr = /*#__PURE__*/_react["default"].createElement("tr", {
    style: style
  }, children);

  return !lock && needStoreRowHeight ? /*#__PURE__*/_react["default"].createElement(_ResizeObservedRow["default"], {
    onResize: setRowHeight,
    rowIndex: rowIndex
  }, tr) : tr;
};

TableHeaderRow.displayName = 'TableHeaderRow';

var _default = (0, _mobxReactLite.observer)(TableHeaderRow);

exports["default"] = _default;
//# sourceMappingURL=TableHeaderRow.js.map
