"use strict";

exports.__esModule = true;
exports.hoveredAnnEasyStore = exports["default"] = exports.HoveredIdContext = void 0;
exports.withHoveredIdFromContext = withHoveredIdFromContext;
var _classnames = _interopRequireDefault(require("classnames"));
var _redux = require("redux");
var _reactRedux = require("react-redux");
var _react = _interopRequireDefault(require("react"));
var _reactEasyState = require("@risingstack/react-easy-state");
var hoveredAnnotationActions = _interopRequireWildcard(require("../redux/hoveredAnnotation"));
var _recompose = require("recompose");
var _sequenceUtils = require("@teselagen/sequence-utils");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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); }
var HoveredIdContext = _react["default"].createContext({
  hoveredId: "" // default value
});
exports.HoveredIdContext = HoveredIdContext;
function withHoveredIdFromContext(Component) {
  return function HoveredIdComponent(props) {
    return /*#__PURE__*/_react["default"].createElement(HoveredIdContext.Consumer, null, function (contexts) {
      return /*#__PURE__*/_react["default"].createElement(Component, _extends({}, props, contexts));
    });
  };
}
var hoveredAnnEasyStore = (0, _reactEasyState.store)({
  hoveredAnn: undefined,
  selectedAnn: undefined
});
exports.hoveredAnnEasyStore = hoveredAnnEasyStore;
var _default = (0, _redux.compose)(withHoveredIdFromContext, (0, _recompose.branch)(function (_ref) {
  var noRedux = _ref.noRedux;
  return !noRedux;
}, (0, _reactRedux.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 = (0, _classnames["default"])(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)), (0, _recompose.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 (_sequenceUtils.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
      });
    };
  }
}));
exports["default"] = _default;