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 { getFeatureToColorMap } from "@teselagen/sequence-utils";
import "./style.css";
import forEach from "lodash/forEach";
import React from "react";
import IntervalTree from "node-interval-tree";
import getXStartAndWidthOfRowAnnotation from "../getXStartAndWidthOfRowAnnotation";
import { getAnnotationRangeType } from "@teselagen/range-utils";
import AnnotationContainerHolder from "../AnnotationContainerHolder";
import AnnotationPositioner from "../AnnotationPositioner";
import PointedAnnotation from "./PointedAnnotation";
import { getBasesToShow } from "./primerBases";
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 IntervalTree(Math.ceil(bpsPerRow / 2));
  var InnerCompToUse = InnerComp || PointedAnnotation;
  if (annotationRanges.length === 0) {
    return null;
  }
  var maxAnnotationYOffset = 0;
  var annotationsSVG = [];
  var extraHeightForContainer = 0;
  forEach(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 && getFeatureToColorMap({
      includeHidden: true
    })[annotation.type] || "#BBBBBB";
    var result = getXStartAndWidthOfRowAnnotation(annotationRange, bpsPerRow, charWidth, gapsBefore, gapsInside);
    var top = annotationRange.yOffset * annotationHeight;
    if (!disregardLocations && annotationRange.containsLocations) {
      top += annotationHeight / 2 - annotationHeight / 16;
    }
    var _getBasesToShow = 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.createElement(AnnotationPositioner, {
      yOffset: annotationRange.yOffset,
      height: anotationHeightNoSpace + extraHeight,
      width: result.width,
      key: index,
      top: top,
      left: result.xStart
    }, /*#__PURE__*/React.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: 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.createElement(AnnotationContainerHolder, {
    marginTop: marginTop,
    marginBottom: marginBottom,
    className: containerClassName,
    containerHeight: containerHeight,
    extraMargin: extraHeightForContainer
  }, annotationsSVG);
}
export default StackedAnnotations;