"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _lodash = require("lodash");
var _reactEasyState = require("@risingstack/react-easy-state");
var _getVisibleStartEnd2 = require("../utils/getVisibleStartEnd");
var _constants = require("./constants");
var _dnaToColor = _interopRequireWildcard(require("../constants/dnaToColor"));
var _withHover = require("../helperComponents/withHover");
var _rangeUtils = require("@teselagen/range-utils");
var _partOverhangs = require("./partOverhangs");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
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); }
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 _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); }
var getChunk = function getChunk(sequence, chunkSize, chunkNumber) {
  return sequence.slice(chunkSize * chunkNumber, chunkSize * (chunkNumber + 1));
};
var Sequence = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(Sequence, _React$Component);
  function Sequence() {
    return _React$Component.apply(this, arguments) || this;
  }
  var _proto = Sequence.prototype;
  _proto.render = function render() {
    var _this$props = this.props,
      isReplacementLayer = _this$props.isReplacementLayer,
      sequence = _this$props.sequence,
      hideBps = _this$props.hideBps,
      charWidth = _this$props.charWidth,
      _this$props$container = _this$props.containerStyle,
      containerStyle = _this$props$container === void 0 ? {} : _this$props$container,
      children = _this$props.children,
      isReverse = _this$props.isReverse,
      height = _this$props.height,
      className = _this$props.className,
      _this$props$startOffs = _this$props.startOffset,
      startOffset = _this$props$startOffs === void 0 ? 0 : _this$props$startOffs,
      _this$props$chunkSize = _this$props.chunkSize,
      chunkSize = _this$props$chunkSize === void 0 ? 100 : _this$props$chunkSize,
      scrollData = _this$props.scrollData,
      showDnaColors = _this$props.showDnaColors,
      fivePrimeThreePrimeHints = _this$props.fivePrimeThreePrimeHints,
      alignmentData = _this$props.alignmentData,
      sequenceLength = _this$props.sequenceLength,
      rowStart = _this$props.rowStart,
      rowEnd = _this$props.rowEnd;

    // the fudge factor is used to position the sequence in the middle of the <text> element
    var fudge = charWidth - _constants.realCharWidth;
    var gapsBeforeSequence = 0;
    var seqReadWidth = 0;
    var rowSeqLen = sequence.length;
    var overlapToBold;
    var isDigestPart;
    [_withHover.hoveredAnnEasyStore.hoveredAnn].forEach(function (ann) {
      if (ann && !isReplacementLayer) {
        var start = ann.start;
        var end = ann.end - 1;
        _partOverhangs.partOverhangs.forEach(function (h) {
          if (ann[h]) {
            isDigestPart = true;
            if (h.includes("Underhang") && isReverse) {
              return;
            } else if (h.includes("Overhang") && !isReverse) {
              return;
            }
            var overhangBps = ann[h];
            if (h.includes("fivePrime")) {
              start = start + overhangBps.length;
            } else {
              end = end - overhangBps.length;
            }
          }
        });
        overlapToBold = isDigestPart ? (0, _rangeUtils.getOverlapsOfPotentiallyCircularRanges)({
          start: start,
          end: end
        }, {
          start: rowStart,
          end: rowEnd
        }, sequenceLength) : undefined;
      }
    });
    var style = _extends({
      position: "relative",
      height: height,
      left: gapsBeforeSequence * charWidth
    }, containerStyle);
    var width = rowSeqLen * charWidth;
    var inner;
    var shared = {
      y: height - height / 4,
      className: "ve-monospace-font " + (isReverse ? " ve-sequence-reverse" : "")
    };
    if (scrollData) {
      var numChunks = Math.ceil(rowSeqLen / chunkSize);
      var chunkWidth = chunkSize * charWidth;
      //we're in the alignment view alignments only
      var _getVisibleStartEnd = (0, _getVisibleStartEnd2.getVisibleStartEnd)({
          scrollData: scrollData,
          width: width
        }),
        visibleStart = _getVisibleStartEnd.visibleStart,
        visibleEnd = _getVisibleStartEnd.visibleEnd;
      inner = (0, _lodash.times)(numChunks, function (i) {
        var seqChunk = getChunk(sequence, chunkSize, i);
        var textLength = charWidth * seqChunk.length;
        var x = i * chunkWidth;
        if (x > visibleEnd || x + textLength < visibleStart) return null;
        return /*#__PURE__*/_react["default"].createElement("text", _extends({
          key: i
        }, _extends({}, shared, {
          textLength: textLength - fudge - _constants.fudge2,
          x: x + fudge / 2,
          lengthAdjust: "spacing"
        })), seqChunk);
      });
    } else {
      inner = /*#__PURE__*/_react["default"].createElement("text", _extends({}, shared, {
        x: 0 + fudge / 2,
        textLength: (alignmentData ? seqReadWidth : width) - fudge - _constants.fudge2
      }), getBoldRegion({
        sequence: sequence,
        overlapToBold: overlapToBold,
        rowStart: rowStart,
        sequenceLength: sequenceLength
      }));
    }
    return /*#__PURE__*/_react["default"].createElement("div", {
      style: style,
      className: (className ? className : "") + " ve-row-item-sequence"
    }, fivePrimeThreePrimeHints && /*#__PURE__*/_react["default"].createElement("div", {
      className: "tg-" + (isReverse ? "left" : "right") + "-prime-direction tg-prime-direction"
    }, "3'"), fivePrimeThreePrimeHints && /*#__PURE__*/_react["default"].createElement("div", {
      className: "tg-" + (isReverse ? "right" : "left") + "-prime-direction tg-prime-direction"
    }, "5'"), !hideBps && /*#__PURE__*/_react["default"].createElement("svg", {
      style: {
        left: startOffset * charWidth,
        height: height,
        width: width,
        position: "absolute"
      },
      ref: "rowViewTextContainer",
      className: "rowViewTextContainer",
      height: Math.max(0, Number(height))
    }, showDnaColors && /*#__PURE__*/_react["default"].createElement(ColoredSequence, _extends({}, this.props, {
      fudge: fudge,
      totalWidth: width
    })), inner), children);
  };
  return Sequence;
}(_react["default"].Component);
var _default = (0, _reactEasyState.view)(Sequence);
exports["default"] = _default;
var ColoredSequence = /*#__PURE__*/function (_React$Component2) {
  _inheritsLoose(ColoredSequence, _React$Component2);
  function ColoredSequence() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$Component2.call.apply(_React$Component2, [this].concat(args)) || this;
    _defineProperty(_assertThisInitialized(_this), "drawRects", function () {
      var _this$props2 = _this.props,
        charWidth = _this$props2.charWidth,
        sequence = _this$props2.sequence,
        height = _this$props2.height,
        isReverse = _this$props2.isReverse,
        alignmentData = _this$props2.alignmentData,
        getGaps = _this$props2.getGaps,
        fudge = _this$props2.fudge,
        totalWidth = _this$props2.totalWidth;
      if (alignmentData) {
        sequence = sequence.replace(/^-+/g, "").replace(/-+$/g, "");
      }
      //we use big paths instead of many individual rects to improve performance
      var colorPaths = Object.values(_dnaToColor["default"]).reduce(function (acc, color) {
        acc[color] = "";
        return acc;
      }, {});
      var gapsBefore = getGaps ? getGaps({
        start: 0,
        end: 0
      }).gapsBefore : 0;
      sequence.split("").forEach(function (_char, i) {
        var width = Number(charWidth);
        var color = (0, _dnaToColor.getDnaColor)(_char, isReverse);
        var x = (i + gapsBefore) * charWidth;
        var y = 0;
        colorPaths[color] = (colorPaths[color] || "") + ("M" + x + "," + y + " L" + (x + width) + "," + y + " L" + (x + width) + "," + (y + height) + " L" + x + "," + (y + height));
      });
      var scalex = (totalWidth - fudge) / totalWidth;
      return /*#__PURE__*/_react["default"].createElement("g", {
        style: {
          transform: "scaleX(" + scalex + ")"
        }
      }, (0, _lodash.map)(colorPaths, function (d, color) {
        return /*#__PURE__*/_react["default"].createElement("path", {
          transform: "tran",
          key: color,
          d: d,
          fill: color
        });
      }));
    });
    return _this;
  }
  var _proto2 = ColoredSequence.prototype;
  _proto2.shouldComponentUpdate = function shouldComponentUpdate(newProps) {
    var props = this.props;
    if (["charWidth", "sequence", "height", "isReverse", "width"].some(function (key) {
      return props[key] !== newProps[key];
    })) return true;
    if (!!props.alignmentData !== !!newProps.alignmentData) return true;
    return false;
  };
  _proto2.render = function render() {
    return this.drawRects();
  };
  return ColoredSequence;
}(_react["default"].Component);
function getBoldRegion(_ref) {
  var sequence = _ref.sequence,
    overlapToBold = _ref.overlapToBold,
    rowStart = _ref.rowStart,
    sequenceLength = _ref.sequenceLength;
  var toRet = [];
  var _ref2 = overlapToBold || [],
    a = _ref2[0],
    b = _ref2[1];
  for (var index = rowStart; index < sequence.length + rowStart; index++) {
    var element = sequence[index - rowStart];
    if (a && (0, _rangeUtils.isPositionWithinRange)(index, a, sequenceLength, true, true) || b && (0, _rangeUtils.isPositionWithinRange)(index, b, sequenceLength, true, true)) {
      toRet.push( /*#__PURE__*/_react["default"].createElement("tspan", {
        className: "isBoldSeq"
      }, element));
    } else {
      toRet.push(element);
    }
  }
  return toRet;
}
module.exports = exports.default;