"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends = Object.assign || 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; };

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _recompose = require("recompose");

var _withHover = require("../helperComponents/withHover");

var _withHover2 = _interopRequireDefault(_withHover);

var _getXStartAndWidthOfRowAnnotation = require("./getXStartAndWidthOfRowAnnotation");

var _getXStartAndWidthOfRowAnnotation2 = _interopRequireDefault(_getXStartAndWidthOfRowAnnotation);

var _nodeIntervalTree = require("node-interval-tree");

var _nodeIntervalTree2 = _interopRequireDefault(_nodeIntervalTree);

var _getYOffset = require("../CircularView/getYOffset");

var _getYOffset2 = _interopRequireDefault(_getYOffset);

var _forEach = require("lodash/forEach");

var _forEach2 = _interopRequireDefault(_forEach);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function CutsiteLabels(props) {
  var _props$annotationRang = props.annotationRanges,
      annotationRanges = _props$annotationRang === undefined ? {} : _props$annotationRang,
      bpsPerRow = props.bpsPerRow,
      charWidth = props.charWidth,
      annotationHeight = props.annotationHeight,
      onClick = props.onClick,
      onRightClick = props.onRightClick,
      _props$textWidth = props.textWidth,
      textWidth = _props$textWidth === undefined ? 10 : _props$textWidth,
      editorName = props.editorName;

  if (annotationRanges.length === 0) {
    return null;
  }
  var warningMessage = null;
  if (Object.keys(annotationRanges).length > 50) {
    warningMessage = _react2.default.createElement(
      "span",
      { style: { color: "red" } },
      _react2.default.createElement("br", null),
      "Warning: Only the first 50 cutsites will be displayed. Filter the cutsites you wish to see using the filter tool ",
      _react2.default.createElement("br", null)
    );
  }
  var rowLength = bpsPerRow * charWidth;
  var counter = 0;
  var maxAnnotationYOffset = 0;
  var annotationsSVG = [];
  var rowCenter = rowLength / 2;
  var iTree = new _nodeIntervalTree2.default(rowCenter);
  (0, _forEach2.default)(annotationRanges, function (annotationRange, index) {
    counter++;
    if (counter > 50) return;
    var annotation = annotationRange.annotation;
    if (!annotation) {
      annotation = annotationRange;
    }
    var annotationLength = annotation.restrictionEnzyme.name.length * textWidth;

    var _getXStartAndWidthOfR = (0, _getXStartAndWidthOfRowAnnotation2.default)(annotationRange, bpsPerRow, charWidth),
        xStart = _getXStartAndWidthOfR.xStart;

    var xStartOriginal = xStart;

    var xEnd = xStart + annotationLength;

    if (xEnd > rowLength) {
      xStart = xStart - (xEnd - rowLength);
      xEnd = rowLength;
    }
    var yOffset = (0, _getYOffset2.default)(iTree, xStart, xEnd);
    iTree.insert(xStart, xEnd, _extends({}, annotationRange, {
      yOffset: yOffset
    }));

    if (yOffset > maxAnnotationYOffset) {
      maxAnnotationYOffset = yOffset;
    }
    var height = yOffset * annotationHeight;
    annotationsSVG.push(_react2.default.createElement(DrawCutsiteLabel, _extends({
      id: annotation.id,
      key: "cutsiteLabel" + index
    }, {
      editorName: editorName,
      annotation: annotation,
      xStartOriginal: xStartOriginal,
      onClick: onClick,
      onRightClick: onRightClick,
      height: height,
      xStart: xStart
    })));
  });
  var containerHeight = (maxAnnotationYOffset + 1) * annotationHeight;
  return _react2.default.createElement(
    "div",
    {
      width: "100%",
      style: {
        position: "relative",
        height: containerHeight,
        display: "block"
      },
      className: "veRowViewCutsiteLabelsContainer"
    },
    annotationsSVG,
    warningMessage
  );
}

exports.default = (0, _recompose.onlyUpdateForKeys)(["annotationRanges", "bpsPerRow", "charWidth", "annotationHeight", "spaceBetweenAnnotations", "onClick", "textWidth", "editorName"])(CutsiteLabels);


var DrawCutsiteLabel = (0, _withHover2.default)(function (_ref) {
  var hovered = _ref.hovered,
      className = _ref.className,
      annotation = _ref.annotation,
      _onClick = _ref.onClick,
      onRightClick = _ref.onRightClick,
      height = _ref.height,
      xStartOriginal = _ref.xStartOriginal,
      onMouseLeave = _ref.onMouseLeave,
      onMouseOver = _ref.onMouseOver,
      xStart = _ref.xStart;

  return _react2.default.createElement(
    "div",
    null,
    _react2.default.createElement(
      "div",
      _extends({ onMouseLeave: onMouseLeave, onMouseOver: onMouseOver }, {
        className: className + " veCutsiteLabel ve-monospace-font",
        onClick: function onClick(event) {
          _onClick({ event: event, annotation: annotation });
          event.stopPropagation();
        },
        onContextMenu: function onContextMenu(event) {
          onRightClick({ event: event, annotation: annotation });
          event.stopPropagation();
        },
        style: _extends({
          cursor: "pointer",
          // left: xStart,
          position: "absolute",
          bottom: height
        }, hovered && { fontWeight: "bold" }, {
          // display: 'inline-block',
          // position: (relative) ? 'relative' : 'absolute',
          // // float: 'left',
          left: xStart,
          zIndex: 10
          // left: '100 % ',
        })
      }),
      annotation.restrictionEnzyme.name
    ),
    hovered && _react2.default.createElement("div", {
      style: {
        position: "absolute",
        left: xStartOriginal,
        bottom: 0,
        height: Math.max(height, 3),
        width: 2,
        background: "black"
      }
    })
  );
});