function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import classnames from "classnames";
import { compose } from "redux";
import { connect } from "react-redux";
import React from "react";
import { store } from "@risingstack/react-easy-state";
import * as hoveredAnnotationActions from "../redux/hoveredAnnotation";
import { withHandlers, branch } from "recompose";
import { modifiableTypes } from "@teselagen/sequence-utils";
export var HoveredIdContext = React.createContext({
  hoveredId: "" // default value
});

export function withHoveredIdFromContext(Component) {
  return function HoveredIdComponent(props) {
    return /*#__PURE__*/React.createElement(HoveredIdContext.Consumer, null, function (contexts) {
      return /*#__PURE__*/React.createElement(Component, _extends({}, props, contexts));
    });
  };
}
export var hoveredAnnEasyStore = store({
  hoveredAnn: undefined,
  selectedAnn: undefined
});
export default compose(withHoveredIdFromContext, branch(function (_ref) {
  var noRedux = _ref.noRedux;
  return !noRedux;
}, connect(function (state, _ref2) {
  var id = _ref2.id,
    _ref2$editorName = _ref2.editorName,
    editorName = _ref2$editorName === void 0 ? "StandaloneEditor" : _ref2$editorName,
    className = _ref2.className,
    hoveredIdFromContext = _ref2.hoveredId,
    passHoveredId = _ref2.passHoveredId;
  if (!editorName) {
    console.warn("please pass an editorName to the withHover() wrapped component");
  }
  var editorState = state.VectorEditor[editorName] || {};
  var hoveredId = editorState.hoveredAnnotation || hoveredIdFromContext; //we can pass a hoveredId from context in order to still use the hover functionality without being connected to redux! see http://localhost:3344/#/SimpleCircularOrLinearView for an example
  var isIdHashmap = typeof id === "object";
  var hovered = !!(isIdHashmap ? id[hoveredId] : hoveredId === id);
  var newClassName = classnames(className, "hoverHelper", {
    veAnnotationHovered: hovered
  });
  var toReturn = {
    hovered: hovered,
    className: newClassName
  };
  if (hovered && passHoveredId) {
    //only pass hoveredId if it is hovered
    toReturn.hoveredId = hoveredId;
  }
  return toReturn;
}, hoveredAnnotationActions)), withHandlers({
  onMouseOver: function onMouseOver(props) {
    return function (e) {
      var _props$label;
      var editorName = props.editorName,
        id = props.id,
        hoveredAnnotationUpdate = props.hoveredAnnotationUpdate;
      var isIdHashmap = typeof id === "object";
      var idToPass = isIdHashmap ? Object.keys(id)[0] : id;
      var annot = (props === null || props === void 0 ? void 0 : props.annotation) || (props === null || props === void 0 ? void 0 : (_props$label = props.label) === null || _props$label === void 0 ? void 0 : _props$label.annotation);
      if (modifiableTypes.includes(annot === null || annot === void 0 ? void 0 : annot.annotationTypePlural)) {
        hoveredAnnEasyStore.hoveredAnn = annot;
      }
      //because the calling onHover can slow things down, we disable it if dragging or scrolling
      if (window.__veDragging || window.__veScrolling) return;
      e.stopPropagation(); //this is important otherwise hovering labels inside circular view label groups won't work
      hoveredAnnotationUpdate && hoveredAnnotationUpdate(idToPass, {
        editorName: editorName
      });
    };
  },
  onMouseLeave: function onMouseLeave(props) {
    return function (e) {
      hoveredAnnEasyStore.hoveredAnn = undefined;
      var editorName = props.editorName,
        hoveredAnnotationClear = props.hoveredAnnotationClear;
      e.stopPropagation();
      if (window.__veDragging || window.__veScrolling) return;
      hoveredAnnotationClear && hoveredAnnotationClear(true, {
        editorName: editorName
      });
    };
  }
}));