import _extends from "@babel/runtime-corejs3/helpers/extends";
import { isLeafNode, makeRecursiveMapper, mergeCellProps } from '../../utils';
var EMPTY_RANGE = {
  start: -1,
  end: -1
};
export 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(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 (!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
            }
          });
        }
      });
    }));
  };
}