"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.columnRangeHover = columnRangeHover;
var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));
var _utils = require("../../utils");
var EMPTY_RANGE = {
  start: -1,
  end: -1
};
function columnRangeHover() {
  var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var stateKey = 'columnHover';
  return function columnRangeHoverStep(pipeline) {
    var _a, _b, _c, _d, _e;
    var hoverRange = (_c = (_b = (_a = opts.hoverRange) !== null && _a !== void 0 ? _a : pipeline.getStateAtKey(stateKey)) !== null && _b !== void 0 ? _b : opts.defaultHoverRange) !== null && _c !== void 0 ? _c : EMPTY_RANGE;
    var hoverColor = (_d = opts.hoverColor) !== null && _d !== void 0 ? _d : 'var(--hover-bgcolor)';
    var headerHoverColor = (_e = opts.headerHoverColor) !== null && _e !== void 0 ? _e : 'var(--header-hover-bgcolor)';
    var onChangeHoverRange = function onChangeHoverRange(nextColIndexRange) {
      var _a;
      pipeline.setStateAtKey(stateKey, nextColIndexRange);
      (_a = opts.onChangeHoverRange) === null || _a === void 0 ? void 0 : _a.call(opts, nextColIndexRange);
    };
    return pipeline.mapColumns((0, _utils.makeRecursiveMapper)(function (col, _ref) {
      var startIndex = _ref.startIndex,
        endIndex = _ref.endIndex;
      var colRange = {
        start: startIndex,
        end: endIndex
      };
      var match = colRange.end > hoverRange.start && hoverRange.end > colRange.start;
      if (!(0, _utils.isLeafNode)(col)) {
        if (headerHoverColor == null) {
          return col;
        }
        return (0, _extends2.default)((0, _extends2.default)({}, col), {
          headerCellProps: (0, _utils.mergeCellProps)(col.headerCellProps, {
            onMouseEnter: function onMouseEnter() {
              onChangeHoverRange(colRange);
            },
            onMouseLeave: function onMouseLeave() {
              onChangeHoverRange(EMPTY_RANGE);
            },
            style: {
              '--header-bgcolor': match ? headerHoverColor : undefined
            }
          })
        });
      }
      var prevGetCellProps = col.getCellProps;
      // 预构建 merge 用的 extra 对象，避免在每个 cell 里重复创建
      var onMouseEnter = function onMouseEnter() {
        onChangeHoverRange(colRange);
      };
      var onMouseLeave = function onMouseLeave() {
        onChangeHoverRange(EMPTY_RANGE);
      };
      var cellExtra = {
        onMouseEnter: onMouseEnter,
        onMouseLeave: onMouseLeave,
        style: {
          '--bgcolor': match ? hoverColor : undefined
        }
      };
      return (0, _extends2.default)((0, _extends2.default)({}, col), {
        headerCellProps: (0, _utils.mergeCellProps)(col.headerCellProps, {
          onMouseEnter: onMouseEnter,
          onMouseLeave: onMouseLeave,
          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);
          if (!prevCellProps) return cellExtra;
          return (0, _utils.mergeCellProps)(prevCellProps, cellExtra);
        }
      });
    }));
  };
}