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 { isPositionWithinRange } from "@teselagen/range-utils";
import assign from "lodash/assign";
import React from "react";
import areNonNegativeIntegers from "validate.io-nonnegative-integer-array";
import { getOverlapsOfPotentiallyCircularRanges } from "@teselagen/range-utils";
import getXStartAndWidthOfRangeWrtRow from "./getXStartAndWidthOfRangeWrtRow";
import pureNoFunc from "../utils/pureNoFunc";
import { connectToEditor } from "../withEditorProps";
var snipStyle = {
  height: "100%",
  // background: 'black',
  position: "absolute",
  width: "2px"
};
var snipConnectorStyle = {
  height: "2px",
  // background: 'black',
  position: "absolute",
  top: -1
};

// var cursor = getCursorForRow(caretPosition, row, bpsPerRow, snipStyle, charWidth, true);

function getSnipForRow(isTopStrand, isHovered, snipPosition, row, sequenceLength, bpsPerRow, snipStyle, charWidth, index) {
  if (!isPositionWithinRange(snipPosition, row)) return;
  var _getXStartAndWidthOfR = getXStartAndWidthOfRangeWrtRow({
      range: {
        start: snipPosition,
        end: snipPosition
      },
      row: row,
      charWidth: charWidth,
      sequenceLength: sequenceLength
    }),
    xStart = _getXStartAndWidthOfR.xStart;
  var newCursorStyle = assign({}, snipStyle, {
    left: xStart
  });
  var cursorEl = /*#__PURE__*/React.createElement("div", {
    key: index,
    className: "veRowViewCutsite snip " + (isTopStrand ? "topStrandSnip" : "bottomStrandSnip") + " " + (isHovered ? "snipHovered" : "") + " snipPosition-" + snipPosition,
    style: newCursorStyle
  });
  return cursorEl;
}
function getSnipConnector(isHovered, snipRange, row, sequenceLength, bpsPerRow, snipConnectorStyle, charWidth, index) {
  //tnr: we basically need to first determine what the range start and end are..
  // var _snipRange = {
  //     ...snipRange,
  //     end: norm(snipRange.end-1,sequenceLength)
  // }
  //then mask the range by the row

  var overlaps = getOverlapsOfPotentiallyCircularRanges(snipRange, _extends({}, row, {
    end: row.end + 1
  }), sequenceLength);
  return overlaps.map(function (overlap, index2) {
    var _getXStartAndWidthOfR2 = getXStartAndWidthOfRangeWrtRow({
        range: overlap,
        row: row,
        charWidth: charWidth,
        sequenceLength: sequenceLength
      }),
      xStart = _getXStartAndWidthOfR2.xStart,
      width = _getXStartAndWidthOfR2.width;
    width -= charWidth;
    //the second logical operator catches the special case where we're at the very end of the sequence..
    var newCursorStyle = assign({}, snipConnectorStyle, {
      left: xStart,
      width: width
    });
    var cursorEl = /*#__PURE__*/React.createElement("div", {
      key: index + index2,
      className: "veRowViewCutsite snipConnector " + (isHovered ? "snipHovered" : "") + " ",
      style: newCursorStyle
    });
    return cursorEl;
  });
}
function Cutsites(props) {
  var annotationRanges = props.annotationRanges,
    charWidth = props.charWidth,
    bpsPerRow = props.bpsPerRow,
    row = props.row,
    sequenceLength = props.sequenceLength,
    topStrand = props.topStrand,
    hoveredAnnotation = props.hoveredAnnotation;
  var snips = [];
  var snipConnectors = [];
  Object.keys(annotationRanges).sort(function (a) {
    return annotationRanges[a].annotation.id === hoveredAnnotation ? 1 : -1;
  }).forEach(function (key) {
    var annotationRange = annotationRanges[key];
    var annotation = annotationRange.annotation;
    if (!annotation) {
      annotation = annotationRange;
    }
    var _annotation = annotation,
      topSnipPosition = _annotation.topSnipPosition,
      bottomSnipPosition = _annotation.bottomSnipPosition,
      upstreamBottomSnip = _annotation.upstreamBottomSnip,
      upstreamTopSnip = _annotation.upstreamTopSnip,
      upstreamTopBeforeBottom = _annotation.upstreamTopBeforeBottom,
      topSnipBeforeBottom = _annotation.topSnipBeforeBottom;
    topSnipPosition = topSnipPosition && Number(topSnipPosition);
    bottomSnipPosition = bottomSnipPosition && Number(bottomSnipPosition);
    upstreamTopSnip = upstreamTopSnip && Number(upstreamTopSnip);
    upstreamBottomSnip = upstreamBottomSnip && Number(upstreamBottomSnip);
    var isHovered = annotation.id === hoveredAnnotation;
    snipStyle = _extends({}, snipStyle, {
      background: annotation.restrictionEnzyme.color
    });
    snipConnectorStyle = _extends({}, snipConnectorStyle, {
      background: annotation.restrictionEnzyme.color
    });
    var newSnip;
    var newConnector;
    var snipRange = {};
    if (areNonNegativeIntegers([bottomSnipPosition, topSnipPosition])) {
      if (topStrand) {
        newSnip = getSnipForRow(true, isHovered, topSnipPosition, row, sequenceLength, bpsPerRow, snipStyle, charWidth, key + "downstream");
        if (newSnip) {
          snips.push(newSnip);
        }
      } else {
        newSnip = getSnipForRow(false, isHovered, bottomSnipPosition, row, sequenceLength, bpsPerRow, snipStyle, charWidth, key + "downstream");
        if (newSnip) {
          snips.push(newSnip);
        }
        if (topSnipBeforeBottom) {
          snipRange.start = topSnipPosition;
          snipRange.end = bottomSnipPosition;
        } else {
          snipRange.start = bottomSnipPosition;
          snipRange.end = topSnipPosition;
        }
        newConnector = getSnipConnector(isHovered, snipRange, row, sequenceLength, bpsPerRow, snipConnectorStyle, charWidth, key + "downstreamConnector");
        snipConnectors.push(newConnector);
      }
    }
    if (areNonNegativeIntegers([upstreamBottomSnip, upstreamTopSnip])) {
      if (topStrand) {
        newSnip = getSnipForRow(true, isHovered, upstreamTopSnip, row, sequenceLength, bpsPerRow, snipStyle, charWidth, key + "upstream");
        if (newSnip) {
          snips.push(newSnip);
        }
      } else {
        newSnip = getSnipForRow(false, isHovered, upstreamBottomSnip, row, sequenceLength, bpsPerRow, snipStyle, charWidth, key + "upstream");
        if (newSnip) {
          snips.push(newSnip);
        }
        if (upstreamTopBeforeBottom) {
          snipRange.start = upstreamTopSnip;
          snipRange.end = upstreamBottomSnip;
        } else {
          snipRange.start = upstreamBottomSnip;
          snipRange.end = upstreamTopSnip;
        }
        newConnector = getSnipConnector(isHovered, snipRange, row, sequenceLength, bpsPerRow, snipConnectorStyle, charWidth, key + "upstreamConnector");
        snipConnectors.push(newConnector);
      }
    }
  });
  return /*#__PURE__*/React.createElement("div", null, snips, snipConnectors);
}
export default pureNoFunc(connectToEditor(function (_ref) {
  var hoveredAnnotation = _ref.hoveredAnnotation;
  return {
    hoveredAnnotation: hoveredAnnotation
  };
})(Cutsites));