import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { useCallback, useContext } from 'react';
import { observer } from 'mobx-react-lite';
import classNames from 'classnames';
import { pxToRem } from '../../../es/_util/UnitConvertor';
import { defaultAggregationRenderer } from './Column';
import TableContext from './TableContext';
import ExpandIcon from './ExpandIcon';

var TableGroupCellInner = function TableGroupCellInner(props) {
  var _useContext = useContext(TableContext),
      dataSet = _useContext.dataSet,
      rowHeight = _useContext.rowHeight,
      indentSize = _useContext.indentSize,
      tableStore = _useContext.tableStore,
      prefixCls = _useContext.prefixCls;

  var column = props.column,
      group = props.group,
      aggregationList = props.children,
      rowSpan = props.rowSpan,
      isFixedRowHeight = props.isFixedRowHeight;
  var children = group.children;
  var _column$renderer = column.renderer,
      renderer = _column$renderer === void 0 ? defaultAggregationRenderer : _column$renderer,
      __tableGroup = column.__tableGroup;
  var handleExpandChange = useCallback(function () {
    tableStore.setGroupExpanded(group, !tableStore.isGroupExpanded(group));
  }, [tableStore, group]);
  var record = group.totalRecords[0];
  var text = renderer({
    text: aggregationList || group.value,
    rowGroup: group,
    dataSet: dataSet,
    record: record
  });
  var hasExpandIcon = Boolean(__tableGroup && __tableGroup.parentField);

  var renderExpandIcon = function renderExpandIcon() {
    if (hasExpandIcon) {
      var expandable = !!children;
      var expandIcon = tableStore.expandIcon;
      var isExpanded = tableStore.isGroupExpanded(group);

      if (typeof expandIcon === 'function') {
        return expandIcon({
          prefixCls: prefixCls,
          expanded: isExpanded,
          expandable: expandable,
          needIndentSpaced: false,
          record: record,
          onExpand: handleExpandChange
        });
      }

      return /*#__PURE__*/React.createElement(ExpandIcon, {
        prefixCls: prefixCls,
        expandable: expandable,
        onChange: handleExpandChange,
        expanded: isExpanded
      });
    }
  };

  var renderIndent = function renderIndent() {
    if (hasExpandIcon) {
      return /*#__PURE__*/React.createElement("span", {
        key: "indent",
        style: {
          paddingLeft: pxToRem(indentSize * group.level)
        }
      });
    }
  };

  var cellProps = {
    className: classNames("".concat(prefixCls, "-cell-inner"), _defineProperty({}, "".concat(prefixCls, "-cell-inner-row-height-fixed"), rowHeight !== 'auto'))
  };

  if (rowSpan === undefined && isFixedRowHeight && rowHeight !== 'auto') {
    cellProps.style = {
      height: pxToRem(rowHeight),
      lineHeight: pxToRem(rowHeight - 2)
    };
  }

  return /*#__PURE__*/React.createElement("span", _extends({}, cellProps), renderIndent(), renderExpandIcon(), text);
};

TableGroupCellInner.displayName = 'TableGroupCellInner';
export default observer(TableGroupCellInner);
//# sourceMappingURL=TableGroupCellInner.js.map
