/* @flow */
import getRangeAngles from "./getRangeAnglesSpecial";
import PositionAnnotationOnCircle from "./PositionAnnotationOnCircle";
import React from "react";
import each from "lodash/each";
import withHover from "../helperComponents/withHover";
import pureNoFunc from "../utils/pureNoFunc";
function Cutsites(_ref) {
  var radius = _ref.radius,
    noRedux = _ref.noRedux,
    editorName = _ref.editorName,
    showCutsiteLabels = _ref.showCutsiteLabels,
    cutsiteClicked = _ref.cutsiteClicked,
    cutsiteDoubleClicked = _ref.cutsiteDoubleClicked,
    cutsiteRightClicked = _ref.cutsiteRightClicked,
    cutsites = _ref.cutsites,
    _ref$cutsiteWidth = _ref.cutsiteWidth,
    cutsiteWidth = _ref$cutsiteWidth === void 0 ? 1 : _ref$cutsiteWidth,
    _ref$annotationHeight = _ref.annotationHeight,
    annotationHeight = _ref$annotationHeight === void 0 ? 15 : _ref$annotationHeight,
    sequenceLength = _ref.sequenceLength;
  radius += annotationHeight;
  var svgGroup = [];
  var labels = {};
  var index = 0;
  if (!Object.keys(cutsites).length) return null;
  each(cutsites, function (annotation /* key */) {
    index++;
    if (!(annotation.topSnipPosition > -1)) {
      //we need this to be present
    }
    var _getRangeAngles = getRangeAngles({
        start: annotation.topSnipPosition,
        end: annotation.topSnipPosition
      }, sequenceLength),
      startAngle = _getRangeAngles.startAngle;
    if (showCutsiteLabels) {
      //expand the end angle if annotation spans the origin
      labels[annotation.id] = {
        annotationCenterAngle: startAngle,
        annotationCenterRadius: radius,
        text: annotation.restrictionEnzyme.name,
        color: annotation.restrictionEnzyme.color,
        className: " veCutsiteLabel",
        id: annotation.id,
        onClick: function onClick(event) {
          cutsiteClicked({
            event: event,
            annotation: annotation
          });
          event.stopPropagation();
        },
        onDoubleClick: function onDoubleClick(event) {
          cutsiteDoubleClicked({
            event: event,
            annotation: annotation
          });
          event.stopPropagation();
        },
        onContextMenu: function onContextMenu(event) {
          cutsiteRightClicked({
            event: event,
            annotation: annotation
          });
          event.stopPropagation();
        }
      };
    }
    svgGroup.push( /*#__PURE__*/React.createElement(DrawCutsite, {
      key: "cutsite" + index,
      noRedux: noRedux,
      editorName: editorName,
      id: annotation.id,
      startAngle: startAngle,
      radius: radius,
      cutsiteWidth: cutsiteWidth,
      annotationHeight: annotationHeight
    }));
  });
  return {
    height: annotationHeight,
    labels: labels,
    component: /*#__PURE__*/React.createElement("g", {
      key: "cutsites",
      className: "cutsites"
    }, svgGroup)
  };
}
export default Cutsites;
var DrawCutsite = pureNoFunc(withHover(function (_ref2) {
  var className = _ref2.className,
    startAngle = _ref2.startAngle,
    radius = _ref2.radius,
    cutsiteWidth = _ref2.cutsiteWidth,
    annotationHeight = _ref2.annotationHeight,
    onMouseLeave = _ref2.onMouseLeave,
    onMouseOver = _ref2.onMouseOver;
  var _PositionAnnotationOn = PositionAnnotationOnCircle({
      sAngle: startAngle,
      eAngle: startAngle,
      height: radius
    }),
    transform = _PositionAnnotationOn.transform;
  return /*#__PURE__*/React.createElement("rect", {
    onMouseLeave: onMouseLeave,
    onMouseOver: onMouseOver,
    className: className,
    transform: transform,
    width: cutsiteWidth,
    height: annotationHeight
  });
}));