import _extends from "@babel/runtime/helpers/extends";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["header", "className", "children", "classPrefix", "headerHeight", "verticalAlign", "width", "left", "headerLeft"];
import React, { isValidElement, useContext } from 'react';
import classNames from 'classnames';
import isString from 'lodash/isString';
import { defaultClassPrefix, prefix } from './utils';
import TableContext from './TableContext';
var ColumnGroup = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var header = props.header,
      className = props.className,
      children = props.children,
      classPrefix = props.classPrefix,
      _props$headerHeight = props.headerHeight,
      headerHeight = _props$headerHeight === void 0 ? 80 : _props$headerHeight,
      verticalAlign = props.verticalAlign,
      width = props.width,
      left = props.left,
      _props$headerLeft = props.headerLeft,
      headerLeft = _props$headerLeft === void 0 ? 0 : _props$headerLeft,
      rest = _objectWithoutProperties(props, _excluded);

  var _useContext = useContext(TableContext),
      scrollX = _useContext.scrollX,
      _useContext$tableWidt = _useContext.tableWidth,
      tableWidth = _useContext$tableWidt === void 0 ? 0 : _useContext$tableWidt,
      tableStore = _useContext.tableStore;

  var originalColumns = tableStore.originalColumns;
  var height = headerHeight / 2;
  var styles = {
    height: height,
    width: width,
    left: left
  };

  var contentStyles = _objectSpread(_objectSpread({}, styles), {}, {
    verticalAlign: verticalAlign
  });

  var addPrefix = function addPrefix(name) {
    return prefix(classPrefix)(name);
  }; // 组合列 header 随滚动条滚动居中。


  var headerMiddleStyle = undefined;
  var fixedWidth = originalColumns.reduce(function (prev, current) {
    if (current.fixed === true || current.fixed === 'left') {
      prev.leftWidth = prev.leftWidth + (current.width || 0);
    } else if (current.fixed === 'right') {
      prev.rightWidth = prev.rightWidth + (current.width || 0);
    }

    return prev;
  }, {
    leftWidth: 0,
    rightWidth: 0
  });
  var mathScrollX = Math.abs(scrollX);
  var calcTableWidth = tableWidth - fixedWidth.rightWidth;
  var mathPostionLeft = headerLeft - calcTableWidth;
  var leftDistanceFixed = headerLeft - fixedWidth.leftWidth;
  var positionLeft = 0;
  var beyondWidth = 0;

  if (mathScrollX > leftDistanceFixed) {
    beyondWidth = mathScrollX - leftDistanceFixed;
  }

  if (mathScrollX > mathPostionLeft && width) {
    var percent = calcTableWidth + mathScrollX - headerLeft + (beyondWidth > 0 ? beyondWidth : 0);

    if (mathScrollX + calcTableWidth > headerLeft + width) {
      percent -= mathScrollX + calcTableWidth - (headerLeft + width);
    }

    positionLeft = percent > 0 && percent / 2 < width ? percent / 2 : 0;
  }

  headerMiddleStyle = {
    position: 'absolute',
    top: '50%',
    left: positionLeft ? positionLeft : '50%',
    transform: 'translate(-50%, -50%)'
  };
  return /*#__PURE__*/React.createElement("div", _extends({
    ref: ref,
    className: classNames(classPrefix, className)
  }, rest), /*#__PURE__*/React.createElement("div", {
    className: addPrefix('header'),
    style: styles
  }, /*#__PURE__*/React.createElement("div", {
    className: addPrefix('header-content'),
    style: contentStyles
  }, isString(header) ? /*#__PURE__*/React.createElement("span", {
    style: headerMiddleStyle
  }, header) : /*#__PURE__*/isValidElement(header) ? /*#__PURE__*/React.cloneElement(header) : header)), React.Children.map(children, function (node) {
    var nodeStyles = _objectSpread(_objectSpread({}, node.props.style), {}, {
      top: styles.height,
      left: styles.left
    });

    return /*#__PURE__*/React.cloneElement(node, {
      className: addPrefix('cell'),
      style: nodeStyles,
      children: /*#__PURE__*/React.createElement("span", {
        className: addPrefix('cell-content')
      }, node.props.children)
    });
  }));
});
ColumnGroup.displayName = 'ColumnGroup';
ColumnGroup.__PRO_TABLE_COLUMN_GROUP = true;
ColumnGroup.defaultProps = {
  headerHeight: 80,
  classPrefix: defaultClassPrefix('performance-table-column-group')
};
export default ColumnGroup;
//# sourceMappingURL=ColumnGroup.js.map
