import _extends from "@babel/runtime-corejs3/helpers/extends";
import { isLeafNode, makeRecursiveMapper, mergeCellProps } from '../../utils';
export function columnHover() {
  var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var stateKey = 'columnHover';
  return function (pipeline) {
    var _a, _b, _c, _d;

    var hoverColor = (_a = opts.hoverColor) !== null && _a !== void 0 ? _a : 'var(--hover-bgcolor)';
    var hoverColIndex = (_d = (_c = (_b = opts.hoverColIndex) !== null && _b !== void 0 ? _b : pipeline.getStateAtKey(stateKey)) !== null && _c !== void 0 ? _c : opts.defaultHoverColIndex) !== null && _d !== void 0 ? _d : -1;

    var onChangeHoverColIndex = function onChangeHoverColIndex(nextColIndex) {
      var _a;

      pipeline.setStateAtKey(stateKey, nextColIndex);
      (_a = opts.onChangeHoverColIndex) === null || _a === void 0 ? void 0 : _a.call(opts, nextColIndex);
    };

    return pipeline.mapColumns(makeRecursiveMapper(function (col, _ref) {
      var startIndex = _ref.startIndex,
          endIndex = _ref.endIndex;
      var range = {
        start: startIndex,
        end: endIndex
      };

      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);
            }
          });
        }
      });
    }));
  };
}