"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _rangeUtils = require("@teselagen/range-utils");
var _assign = _interopRequireDefault(require("lodash/assign"));
var _react = _interopRequireDefault(require("react"));
var _validate = _interopRequireDefault(require("validate.io-nonnegative-integer-array"));
var _getXStartAndWidthOfRangeWrtRow = _interopRequireDefault(require("./getXStartAndWidthOfRangeWrtRow"));
var _pureNoFunc = _interopRequireDefault(require("../utils/pureNoFunc"));
var _withEditorProps = require("../withEditorProps");
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 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 (!(0, _rangeUtils.isPositionWithinRange)(snipPosition, row)) return;
  var _getXStartAndWidthOfR = (0, _getXStartAndWidthOfRangeWrtRow["default"])({
      range: {
        start: snipPosition,
        end: snipPosition
      },
      row: row,
      charWidth: charWidth,
      sequenceLength: sequenceLength
    }),
    xStart = _getXStartAndWidthOfR.xStart;
  var newCursorStyle = (0, _assign["default"])({}, snipStyle, {
    left: xStart
  });
  var cursorEl = /*#__PURE__*/_react["default"].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 = (0, _rangeUtils.getOverlapsOfPotentiallyCircularRanges)(snipRange, _extends({}, row, {
    end: row.end + 1
  }), sequenceLength);
  return overlaps.map(function (overlap, index2) {
    var _getXStartAndWidthOfR2 = (0, _getXStartAndWidthOfRangeWrtRow["default"])({
        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 = (0, _assign["default"])({}, snipConnectorStyle, {
      left: xStart,
      width: width
    });
    var cursorEl = /*#__PURE__*/_react["default"].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 ((0, _validate["default"])([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 ((0, _validate["default"])([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["default"].createElement("div", null, snips, snipConnectors);
}
var _default = (0, _pureNoFunc["default"])((0, _withEditorProps.connectToEditor)(function (_ref) {
  var hoveredAnnotation = _ref.hoveredAnnotation;
  return {
    hoveredAnnotation: hoveredAnnotation
  };
})(Cutsites));
exports["default"] = _default;
module.exports = exports.default;