"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _sequenceUtils = require("@teselagen/sequence-utils");
require("./style.css");
var _forEach = _interopRequireDefault(require("lodash/forEach"));
var _react = _interopRequireDefault(require("react"));
var _nodeIntervalTree = _interopRequireDefault(require("node-interval-tree"));
var _getXStartAndWidthOfRowAnnotation = _interopRequireDefault(require("../getXStartAndWidthOfRowAnnotation"));
var _rangeUtils = require("@teselagen/range-utils");
var _AnnotationContainerHolder = _interopRequireDefault(require("../AnnotationContainerHolder"));
var _AnnotationPositioner = _interopRequireDefault(require("../AnnotationPositioner"));
var _PointedAnnotation = _interopRequireDefault(require("./PointedAnnotation"));
var _primerBases = require("./primerBases");
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); }
function StackedAnnotations(props) {
  var _props$annotationRang = props.annotationRanges,
    annotationRanges = _props$annotationRang === void 0 ? [] : _props$annotationRang,
    bpsPerRow = props.bpsPerRow,
    charWidth = props.charWidth,
    containerClassName = props.containerClassName,
    annotationHeight = props.annotationHeight,
    spaceBetweenAnnotations = props.spaceBetweenAnnotations,
    onClick = props.onClick,
    isProtein = props.isProtein,
    onDoubleClick = props.onDoubleClick,
    disregardLocations = props.disregardLocations,
    InnerComp = props.InnerComp,
    onRightClick = props.onRightClick,
    editorName = props.editorName,
    type = props.type,
    readOnly = props.readOnly,
    noRedux = props.noRedux,
    alignmentType = props.alignmentType,
    getGaps = props.getGaps,
    marginTop = props.marginTop,
    sequenceLength = props.sequenceLength,
    fullSequence = props.fullSequence,
    marginBottom = props.marginBottom,
    truncateLabelsThatDoNotFit = props.truncateLabelsThatDoNotFit,
    getExtraInnerCompProps = props.getExtraInnerCompProps,
    onlyShowLabelsThatDoNotFit = props.onlyShowLabelsThatDoNotFit,
    isStriped = props.isStriped;
  var iTree = new _nodeIntervalTree["default"](Math.ceil(bpsPerRow / 2));
  var InnerCompToUse = InnerComp || _PointedAnnotation["default"];
  if (annotationRanges.length === 0) {
    return null;
  }
  var maxAnnotationYOffset = 0;
  var annotationsSVG = [];
  var extraHeightForContainer = 0;
  (0, _forEach["default"])(annotationRanges, function (annotationRange, index) {
    if (annotationRange.yOffset > maxAnnotationYOffset) {
      maxAnnotationYOffset = annotationRange.yOffset;
    }
    var _getGaps = getGaps(annotationRange),
      gapsBefore = _getGaps.gapsBefore,
      gapsInside = _getGaps.gapsInside;
    if (alignmentType === "Parallel Part Creation") {
      gapsBefore = 0;
      gapsInside = 0;
    }
    var annotation = annotationRange.annotation;
    var annotationColor = annotation.color || annotation.type && (0, _sequenceUtils.getFeatureToColorMap)({
      includeHidden: true
    })[annotation.type] || "#BBBBBB";
    var result = (0, _getXStartAndWidthOfRowAnnotation["default"])(annotationRange, bpsPerRow, charWidth, gapsBefore, gapsInside);
    var top = annotationRange.yOffset * annotationHeight;
    if (!disregardLocations && annotationRange.containsLocations) {
      top += annotationHeight / 2 - annotationHeight / 16;
    }
    var _getBasesToShow = (0, _primerBases.getBasesToShow)({
        hidePrimerBases: charWidth < 8,
        sequenceLength: sequenceLength,
        iTree: iTree,
        fullSequence: fullSequence,
        annotation: annotation,
        annotationRange: annotationRange,
        charWidth: charWidth,
        bpsPerRow: bpsPerRow
      }),
      baseEl = _getBasesToShow.baseEl,
      _getBasesToShow$extra = _getBasesToShow.extraHeight,
      extraHeight = _getBasesToShow$extra === void 0 ? 0 : _getBasesToShow$extra,
      insertPaths = _getBasesToShow.insertPaths,
      insertTicks = _getBasesToShow.insertTicks,
      flipAnnotation = _getBasesToShow.flipAnnotation;
    var anotationHeightNoSpace = annotationHeight - spaceBetweenAnnotations;
    extraHeightForContainer += extraHeight;
    annotationsSVG.push( /*#__PURE__*/_react["default"].createElement(_AnnotationPositioner["default"], {
      yOffset: annotationRange.yOffset,
      height: anotationHeightNoSpace + extraHeight,
      width: result.width,
      key: index,
      top: top,
      left: result.xStart
    }, /*#__PURE__*/_react["default"].createElement(InnerCompToUse, _extends({
      noRedux: noRedux,
      key: index,
      className: annotation.className,
      arrowheadType: annotation.arrowheadType,
      editorName: editorName,
      id: annotation.id,
      fivePrimeOverhang: annotation.fivePrimeOverhang,
      fivePrimeUnderhang: annotation.fivePrimeUnderhang,
      fivePrimeDigestingEnzyme: annotation.fivePrimeDigestingEnzyme,
      threePrimeOverhang: annotation.threePrimeOverhang,
      threePrimeUnderhang: annotation.threePrimeUnderhang,
      threePrimeDigestingEnzyme: annotation.threePrimeDigestingEnzyme,
      onClick: onClick,
      truncateLabelsThatDoNotFit: truncateLabelsThatDoNotFit,
      insertPaths: insertPaths,
      insertTicks: insertTicks,
      onDoubleClick: onDoubleClick,
      type: type,
      readOnly: readOnly,
      flipAnnotation: flipAnnotation,
      extraHeight: extraHeight,
      onRightClick: onRightClick,
      annotation: annotation,
      isProtein: isProtein,
      isStriped: isStriped,
      gapsInside: gapsInside,
      gapsBefore: gapsBefore,
      color: annotationColor,
      width: result.width,
      annotationRange: annotationRange,
      widthInBps: annotationRange.end - annotationRange.start + 1,
      charWidth: charWidth,
      forward: annotation.forward,
      onlyShowLabelsThatDoNotFit: onlyShowLabelsThatDoNotFit,
      rangeType: (0, _rangeUtils.getAnnotationRangeType)(annotationRange, annotation, annotation.forward)
    }, annotation.bases && {
      pointiness: 3,
      arrowPointiness: 0.2
    }, {
      height: annotationRange.containsLocations && !disregardLocations ? anotationHeightNoSpace / 8 : anotationHeightNoSpace,
      hideName: type === "primer" && annotation.bases || annotationRange.containsLocations && !disregardLocations,
      name: annotation.name
    }, getExtraInnerCompProps && getExtraInnerCompProps(annotationRange, props))), baseEl));
  });
  var containerHeight = (maxAnnotationYOffset + 1) * annotationHeight;
  return /*#__PURE__*/_react["default"].createElement(_AnnotationContainerHolder["default"], {
    marginTop: marginTop,
    marginBottom: marginBottom,
    className: containerClassName,
    containerHeight: containerHeight,
    extraMargin: extraHeightForContainer
  }, annotationsSVG);
}
var _default = StackedAnnotations;
exports["default"] = _default;
module.exports = exports.default;