function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import React from "react";
import { getSequenceWithinRange, zeroSubrangeByContainerRange } from "@teselagen/range-utils";
import AASliver from "./AASliver";
import pureNoFunc from "../../utils/pureNoFunc";
import { proteinAlphabet } from "@teselagen/sequence-utils";
var Translation = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(Translation, _React$Component);
  function Translation() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _defineProperty(_assertThisInitialized(_this), "state", {
      hasMounted: false
    });
    return _this;
  }
  var _proto = Translation.prototype;
  _proto.componentDidMount = function componentDidMount() {
    var _this2 = this;
    this.timeout = setTimeout(function () {
      _this2.setState({
        hasMounted: true
      });
    }, 5);
  };
  _proto.componentWillUnmount = function componentWillUnmount() {
    !window.Cypress && clearTimeout(this.timeout);
  };
  _proto.render = function render() {
    var _this$props = this.props,
      annotationRange = _this$props.annotationRange,
      height = _this$props.height,
      showAminoAcidNumbers = _this$props.showAminoAcidNumbers,
      charWidth = _this$props.charWidth,
      aminoAcidNumbersHeight = _this$props.aminoAcidNumbersHeight,
      _onClick = _this$props.onClick,
      onRightClick = _this$props.onRightClick,
      _onDoubleClick = _this$props.onDoubleClick,
      sequenceLength = _this$props.sequenceLength,
      getGaps = _this$props.getGaps,
      colorType = _this$props.colorType,
      isProtein = _this$props.isProtein;
    var hasMounted = this.state.hasMounted;
    var annotation = annotationRange.annotation;
    if (!hasMounted && !isProtein) {
      return /*#__PURE__*/React.createElement("g", {
        height: height,
        className: "translationLayer"
      });
    }
    //we have an amino acid representation of our entire annotation, but it is an array
    //starting at 0, even if the annotation starts at some arbitrary point in the sequence
    var _annotation$aminoAcid = annotation.aminoAcids,
      aminoAcids = _annotation$aminoAcid === void 0 ? [] : _annotation$aminoAcid;
    //so we "zero" our subRange by the annotation start
    var subrangeStartRelativeToAnnotationStart = zeroSubrangeByContainerRange(annotationRange, annotation, sequenceLength);
    //which allows us to then get the amino acids for the subRange
    var aminoAcidsForSubrange = getSequenceWithinRange(subrangeStartRelativeToAnnotationStart, aminoAcids);

    //we then loop over all the amino acids in the sub range and draw them onto the row
    var translationSVG = aminoAcidsForSubrange.map(function (aminoAcidSliver, index) {
      var isEndFiller = index === 0 && aminoAcidSliver.positionInCodon === (annotation.forward ? 2 : 0);
      // const isStartFiller = false
      var isTruncatedEnd = index === 0 && aminoAcidSliver.positionInCodon === 1;
      var isTruncatedStart = index === aminoAcidsForSubrange.length - 1 && aminoAcidSliver.positionInCodon === 1;
      if (!annotation.forward) {
        var holder = isTruncatedEnd;
        isTruncatedEnd = isTruncatedStart;
        isTruncatedStart = holder;
      }
      var isStartFiller = index === aminoAcidsForSubrange.length - 1 && aminoAcidSliver.positionInCodon === (annotation.forward ? 0 : 2);
      if (aminoAcidSliver.positionInCodon !== 1 && !isStartFiller && !isEndFiller) {
        return null;
      }
      var _getGaps = getGaps(aminoAcidSliver.codonRange),
        gapsInside = _getGaps.gapsInside,
        gapsBefore = _getGaps.gapsBefore;
      var gapsInsideFeatureStartToBp = getGaps({
        start: annotationRange.start,
        end: aminoAcidSliver.sequenceIndex
      }).gapsInside;
      // var relativeAAPositionInTranslation = annotationRange.start % bpsPerRow + index;
      var relativeAAPositionInTranslation = index;
      var aminoAcid = aminoAcidSliver.aminoAcid || {};
      //get the codonIndices relative to
      var alphVal = proteinAlphabet[aminoAcid.value];
      var color;
      if (alphVal) {
        color = colorType === "byHydrophobicity" ? alphVal.color : alphVal.colorByFamily;
      } else {
        color = aminoAcid.color;
      }
      return /*#__PURE__*/React.createElement(AASliver, {
        isFiller: isEndFiller || isStartFiller,
        isTruncatedEnd: isTruncatedEnd,
        isTruncatedStart: isTruncatedStart,
        onClick: function onClick(event) {
          _onClick({
            annotation: aminoAcidSliver.codonRange,
            codonRange: aminoAcidSliver.codonRange,
            event: event,
            gapsInside: gapsInside,
            gapsBefore: gapsBefore
          });
        },
        onContextMenu: function onContextMenu(event) {
          onRightClick && onRightClick({
            annotation: annotation,
            codonRange: aminoAcidSliver.codonRange,
            event: event,
            gapsInside: gapsInside,
            gapsBefore: gapsBefore
          });
        },
        title: aminoAcid.name + " -- Index: " + (aminoAcidSliver.aminoAcidIndex + 1) + " -- Hydrophobicity: " + aminoAcid.hydrophobicity + " -- Mass: " + aminoAcid.mass,
        showAminoAcidNumbers: showAminoAcidNumbers,
        aminoAcidIndex: aminoAcidSliver.aminoAcidIndex,
        onDoubleClick: function onDoubleClick(event) {
          _onDoubleClick({
            annotation: annotation,
            event: event
          });
        },
        getGaps: getGaps,
        key: annotation.id + aminoAcidSliver.sequenceIndex,
        forward: annotation.forward,
        width: charWidth,
        height: showAminoAcidNumbers ? height - aminoAcidNumbersHeight : height,
        relativeAAPositionInTranslation: relativeAAPositionInTranslation + gapsInsideFeatureStartToBp,
        letter: aminoAcid.value,
        color: color,
        positionInCodon: aminoAcidSliver.positionInCodon
      });
    });
    return /*#__PURE__*/React.createElement("g", {
      className: "translationLayer"
    }, translationSVG);
  };
  return Translation;
}(React.Component);
export default pureNoFunc(Translation);