import _slicedToArray from "@babel/runtime-corejs3/helpers/slicedToArray";
import _extends from "@babel/runtime-corejs3/helpers/extends";
import { useState } from 'react';
import { isLeafNode, mergeCellProps, traverseColumn } from '../utils';
import { warnTransformsDeprecated } from './warnTransformsDeprecated';
var EMPTY_RANGE = {
  start: -1,
  end: -1
};
/** @deprecated transform 用法已经过时，请使用 pipeline 来对表格进行拓展 */
export function makeColumnRangeHoverTransform(_ref) {
  var _ref$hoverColor = _ref.hoverColor,
    hoverColor = _ref$hoverColor === void 0 ? 'var(--hover-bgcolor)' : _ref$hoverColor,
    _ref$headerHoverColor = _ref.headerHoverColor,
    headerHoverColor = _ref$headerHoverColor === void 0 ? 'var(--header-hover-bgcolor)' : _ref$headerHoverColor,
    hoverRange = _ref.hoverRange,
    onChangeHoverRange = _ref.onChangeHoverRange;
  warnTransformsDeprecated('makeColumnRangeHoverTransform');
  return traverseColumn(function (col, _ref2) {
    var colRange = _ref2.range;
    var match = colRange.end > hoverRange.start && hoverRange.end > colRange.start;
    if (!isLeafNode(col)) {
      if (headerHoverColor == null) {
        return col;
      }
      return _extends(_extends({}, col), {
        headerCellProps: mergeCellProps(col.headerCellProps, {
          onMouseEnter: function onMouseEnter() {
            onChangeHoverRange(colRange);
          },
          onMouseLeave: function onMouseLeave() {
            onChangeHoverRange(EMPTY_RANGE);
          },
          style: {
            '--header-bgcolor': match ? headerHoverColor : undefined
          }
        })
      });
    }
    var prevGetCellProps = col.getCellProps;
    return _extends(_extends({}, col), {
      headerCellProps: mergeCellProps(col.headerCellProps, {
        onMouseEnter: function onMouseEnter() {
          onChangeHoverRange(colRange);
        },
        onMouseLeave: function onMouseLeave() {
          onChangeHoverRange(EMPTY_RANGE);
        },
        style: {
          '--header-bgcolor': match ? headerHoverColor : undefined
        }
      }),
      getCellProps: function getCellProps(value, record, rowIndex) {
        var prevCellProps = prevGetCellProps === null || prevGetCellProps === void 0 ? void 0 : prevGetCellProps(value, record, rowIndex);
        return mergeCellProps(prevCellProps, {
          onMouseEnter: function onMouseEnter() {
            onChangeHoverRange(colRange);
          },
          onMouseLeave: function onMouseLeave() {
            onChangeHoverRange(EMPTY_RANGE);
          },
          style: {
            '--bgcolor': match ? hoverColor : undefined
          }
        });
      }
    });
  });
}
/** @deprecated transform 用法已经过时，请使用 pipeline 来对表格进行拓展 */
export function useColumnHoverRangeTransform() {
  var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
    hoverColor = _ref3.hoverColor,
    headerHoverColor = _ref3.headerHoverColor,
    _ref3$defaultHoverRan = _ref3.defaultHoverRange,
    defaultHoverRange = _ref3$defaultHoverRan === void 0 ? EMPTY_RANGE : _ref3$defaultHoverRan;
  var _useState = useState(defaultHoverRange),
    _useState2 = _slicedToArray(_useState, 2),
    hoverRange = _useState2[0],
    onChangeHoverRange = _useState2[1];
  return makeColumnRangeHoverTransform({
    hoverColor: hoverColor,
    headerHoverColor: headerHoverColor,
    hoverRange: hoverRange,
    onChangeHoverRange: onChangeHoverRange
  });
}