import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["width", "sortable", "children", "dataKey", "onSortColumn", "sortColumn", "sortType", "headerHeight", "groupHeader", "left", "resizable", "fixed"];
import * as React from 'react';
import isFunction from 'lodash/isFunction';
import groupBy from 'lodash/groupBy';
import get from 'lodash/get';
import ColumnGroup from '../ColumnGroup';
import HeaderCell from '../HeaderCell';
import isNullOrUndefined from './isNullOrUndefined';

function cloneCell(Cell, props) {
  return /*#__PURE__*/React.cloneElement(Cell, props);
}

function mergeCells(cells) {
  var leftFixedLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  var nextCells = [];
  var columnGroupLeft = 0;
  var mergeMaxCells = [];
  var currentGroupName = '';
  var usedCells = [];
  var preWidth = [0];

  var _loop = function _loop(_i) {
    var _cells$_i$props = cells[_i].props,
        width = _cells$_i$props.width,
        colSpan = _cells$_i$props.colSpan,
        groupCount = _cells$_i$props.groupCount,
        groupHeader = _cells$_i$props.groupHeader,
        isHeaderCell = _cells$_i$props.isHeaderCell,
        headerHeight = _cells$_i$props.headerHeight,
        verticalAlign = _cells$_i$props.verticalAlign,
        parent = _cells$_i$props.parent,
        cellLeft = _cells$_i$props.left;

    if (usedCells.includes(cells[_i].key)) {
      i = _i;
      return "continue";
    }

    var groupChildrenStore = [];
    var groupChildren = []; // fix(ColumnGroup): fix column cannot be sorted in ColumnGroup

    /**
     * 为列头添加分组
     */

    if (parent && isHeaderCell) {
      var nextWidth = width;
      var left = 0;
      var resizeLeft = 0;

      for (var j = 0; j < groupCount; j += 1) {
        var nextCell = cells[_i + j];

        var _nextCell$props = nextCell.props,
            nextCellWidth = _nextCell$props.width,
            sortable = _nextCell$props.sortable,
            children = _nextCell$props.children,
            dataKey = _nextCell$props.dataKey,
            onSortColumn = _nextCell$props.onSortColumn,
            sortColumn = _nextCell$props.sortColumn,
            sortType = _nextCell$props.sortType,
            _headerHeight = _nextCell$props.headerHeight,
            _nextCell$props$group = _nextCell$props.groupHeader,
            nextGroupHeader = _nextCell$props$group === void 0 ? currentGroupName : _nextCell$props$group,
            columnLeft = _nextCell$props.left,
            resizable = _nextCell$props.resizable,
            _fixed = _nextCell$props.fixed,
            otherProps = _objectWithoutProperties(_nextCell$props, _excluded);

        if (j === 0) {
          currentGroupName = nextGroupHeader;
          resizeLeft = columnLeft;
        }

        if (currentGroupName !== nextGroupHeader) {
          groupChildrenStore = [].concat(groupChildren);
          _i = _i + j;
          break;
        }

        if (j !== 0) {
          nextWidth += nextCellWidth;
          left += cells[_i + j - 1].props.width;
          cells[_i + j] = cloneCell(nextCell, {
            hidden: true
          });
        }

        var otherHeaderProps = void 0;

        if (resizable) {
          otherHeaderProps = {
            resizable: resizable,
            resizeLeft: resizeLeft,
            onColumnResizeEnd: otherProps.onColumnResizeEnd,
            onColumnResizeMove: otherProps.onColumnResizeMove,
            onColumnResizeStart: otherProps.onColumnResizeStart,
            onMouseEnterHandler: otherProps.onMouseEnterHandler,
            onMouseLeaveHandler: otherProps.onMouseLeaveHandler
          };
        }

        groupChildren.push( /*#__PURE__*/React.createElement(HeaderCell, _extends({
          key: j,
          index: leftFixedLength + _i + j,
          left: left,
          fixed: _fixed,
          dataKey: dataKey,
          width: nextCellWidth,
          sortable: sortable,
          sortColumn: sortColumn,
          sortType: sortType,
          onSortColumn: onSortColumn,
          verticalAlign: verticalAlign,
          headerHeight: _headerHeight / 2
        }, otherHeaderProps), children));
        usedCells.push(nextCell.key);
      }

      if (groupHeader !== parent.props.header) {
        if (mergeMaxCells.length && parent.props.header !== mergeMaxCells[mergeMaxCells.length - 1].header) {
          columnGroupLeft = nextWidth;
          preWidth = [0];
        } else {
          columnGroupLeft += nextWidth;
        }

        mergeMaxCells.push(_objectSpread(_objectSpread({
          index: _i,
          content: groupCount ? /*#__PURE__*/React.createElement(ColumnGroup, {
            left: preWidth[preWidth.length - 1],
            width: nextWidth,
            header: groupHeader,
            verticalAlign: verticalAlign,
            headerHeight: headerHeight / 3 * 2,
            headerLeft: cellLeft
          }, groupChildren.map(function (x) {
            return /*#__PURE__*/React.cloneElement(x, _objectSpread(_objectSpread({}, x.props), {}, {
              headerHeight: headerHeight / 3
            }));
          })) : /*#__PURE__*/React.cloneElement(cells[_i], {
            left: preWidth[preWidth.length - 1],
            resizeLeft: cellLeft - preWidth[preWidth.length - 1],
            headerHeight: headerHeight / 3 * 2
          })
        }, parent.props), {}, {
          headerHeight: headerHeight,
          width: nextWidth
        }));
        preWidth.push(columnGroupLeft);
      } else if (groupChildrenStore.length) {
        columnGroupLeft += nextWidth;
        preWidth.push(columnGroupLeft);
        groupChildrenStore.forEach(function (x) {
          mergeMaxCells.push(_objectSpread(_objectSpread({
            index: _i,
            content: /*#__PURE__*/React.cloneElement(x, _objectSpread(_objectSpread({}, x.props), {}, {
              headerHeight: headerHeight * (2 / 3)
            }))
          }, parent.props), {}, {
            headerHeight: headerHeight,
            width: x.props.width
          }));
        });
      } else {
        nextCells.push(cloneCell(cells[_i], {
          width: nextWidth,
          children: /*#__PURE__*/React.createElement(ColumnGroup, {
            width: nextWidth,
            headerHeight: headerHeight,
            header: groupHeader,
            verticalAlign: verticalAlign,
            headerLeft: cellLeft
          }, groupChildren)
        }));
      }

      i = _i;
      return "continue";
    } else if (colSpan) {
      /**
       * 如果存在 colSpan 属性，就去找它的下一个 Cell,
       * 看看值是否是 isNullOrUndefined，，如果为空这可以合并这个单元格
       */
      var _nextWidth = width;

      for (var _j = 0; _j < colSpan; _j += 1) {
        var _nextCell = cells[_i + _j];

        if (_nextCell) {
          var _nextCell$props2 = _nextCell.props,
              rowData = _nextCell$props2.rowData,
              rowIndex = _nextCell$props2.rowIndex,
              _children = _nextCell$props2.children,
              colSpanWidth = _nextCell$props2.width,
              _isHeaderCell = _nextCell$props2.isHeaderCell,
              _dataKey = _nextCell$props2.dataKey;
          var cellText = isFunction(_children) ? _children(rowData, rowIndex) : get(rowData, _dataKey);

          if (rowData && isNullOrUndefined(cellText) || _isHeaderCell && isNullOrUndefined(_children)) {
            _nextWidth += colSpanWidth;
            cells[_i + _j] = cloneCell(_nextCell, {
              hidden: true
            });
          }
        }
      }

      nextCells.push(cloneCell(cells[_i], {
        width: _nextWidth,
        //  Fix this use of the variablecolSpan always evaluates to true
        'aria-colspan': _nextWidth > width ? colSpan : undefined
      }));
      i = _i;
      return "continue";
    }

    nextCells.push(cells[_i]);
    i = _i;
  };

  for (var i = 0; i < cells.length; i += 1) {
    var _ret = _loop(i);

    if (_ret === "continue") continue;
  }

  if (mergeMaxCells.length) {
    var groupByHeader = groupBy(mergeMaxCells, 'header');

    for (var key in groupByHeader) {
      var mapGroup = groupByHeader[key];
      var firstMerge = mapGroup[0];
      var width = mapGroup.reduce(function (total, col) {
        return total += col.width;
      }, 0);
      var align = firstMerge.align,
          verticalAlign = firstMerge.verticalAlign,
          headerHeight = firstMerge.headerHeight,
          header = firstMerge.header,
          fixed = firstMerge.fixed,
          index = firstMerge.index;
      nextCells.push(cloneCell(cells[index], {
        width: width,
        align: align,
        fixed: fixed,
        resizable: false,
        children: /*#__PURE__*/React.createElement(ColumnGroup, {
          width: width,
          headerHeight: headerHeight / 3 * 2,
          header: header,
          verticalAlign: verticalAlign,
          headerLeft: cells[index].props.left
        }, mapGroup.map(function (x) {
          return x.content;
        }))
      }));
    }
  }

  return nextCells;
}

export default mergeCells;
//# sourceMappingURL=mergeCells.js.map
