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';
/** @deprecated transform 用法已经过时，请使用 pipeline 来对表格进行拓展 */

export function makeColumnHoverTransform(_ref) {
  var _ref$hoverColor = _ref.hoverColor,
      hoverColor = _ref$hoverColor === void 0 ? 'var(--hover-bgcolor)' : _ref$hoverColor,
      hoverColIndex = _ref.hoverColIndex,
      onChangeHoverColIndex = _ref.onChangeHoverColIndex;
  warnTransformsDeprecated('makeColumnHoverTransform');
  return traverseColumn(function (col, _ref2) {
    var range = _ref2.range;

    if (!isLeafNode(col)) {
      return col;
    }

    var colIndexMatched = range.start <= hoverColIndex && hoverColIndex < range.end;
    var prevGetCellProps = col.getCellProps;
    return _extends(_extends({}, col), {
      getCellProps: function getCellProps(value, record, rowIndex) {
        var prevCellProps = prevGetCellProps === null || prevGetCellProps === void 0 ? void 0 : prevGetCellProps(value, record, rowIndex);
        return mergeCellProps(prevCellProps, {
          style: {
            '--bgcolor': colIndexMatched ? hoverColor : undefined
          },
          onMouseEnter: function onMouseEnter() {
            onChangeHoverColIndex(range.start);
          },
          onMouseLeave: function onMouseLeave() {
            onChangeHoverColIndex(-1);
          }
        });
      }
    });
  });
}
/** @deprecated transform 用法已经过时，请使用 pipeline 来对表格进行拓展 */

export function useColumnHoverTransform() {
  var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      hoverColor = _ref3.hoverColor,
      _ref3$defaultHoverCol = _ref3.defaultHoverColIndex,
      defaultHoverColIndex = _ref3$defaultHoverCol === void 0 ? -1 : _ref3$defaultHoverCol;

  var _useState = useState(defaultHoverColIndex),
      _useState2 = _slicedToArray(_useState, 2),
      hoverColIndex = _useState2[0],
      onChangeHoverColIndex = _useState2[1];

  return makeColumnHoverTransform({
    hoverColor: hoverColor,
    hoverColIndex: hoverColIndex,
    onChangeHoverColIndex: onChangeHoverColIndex
  });
}