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 { getRangeLength, getSequenceWithinRange } from "@teselagen/range-utils";
import React from "react";
import { divideBy3 } from "./proteinUtils";
import { getInsertBetweenVals, calculatePercentGC, bioData, aliasedEnzymesByName } from "@teselagen/sequence-utils";
import { get, sortBy } from "lodash";
import VeWarning from "../helperComponents/VeWarning";
import { normalizePositionByRangeLength } from "@teselagen/range-utils";
import { filter } from "lodash";
export function getSelectionMessage(_ref) {
  var _ref$caretPosition = _ref.caretPosition,
    caretPosition = _ref$caretPosition === void 0 ? -1 : _ref$caretPosition,
    _ref$selectionLayer = _ref.selectionLayer,
    selectionLayer = _ref$selectionLayer === void 0 ? {
      start: -1,
      end: -1
    } : _ref$selectionLayer,
    customTitle = _ref.customTitle,
    sequenceLength = _ref.sequenceLength,
    sequenceData = _ref.sequenceData,
    showGCContent = _ref.showGCContent,
    GCDecimalDigits = _ref.GCDecimalDigits,
    isProtein = _ref.isProtein;
  var _selectionLayer = selectionLayer;
  var isSelecting = selectionLayer.start > -1;
  if (isSelecting) {
    _selectionLayer = getSelFromWrappedAddon(selectionLayer, sequenceLength);
    var length = getRangeLength(_selectionLayer, sequenceLength);
    var GCContent = function GCContent(numDecimalDigits) {
      if (numDecimalDigits === void 0) {
        numDecimalDigits = 0;
      }
      return calculatePercentGC(getSequenceWithinRange(_selectionLayer, sequenceData.sequence)).toFixed(numDecimalDigits);
    };
    var seqLen = divideBy3(length, isProtein);
    return (customTitle || "选择") + " " + seqLen + " " + ((isProtein ? "AA" : "bp") + (seqLen === 1 ? "" : "s")) + " \u4ECE " + (divideBy3(_selectionLayer.start, isProtein) + 1) + " \u5230 " + divideBy3(_selectionLayer.end + 1, isProtein) + (showGCContent && !isProtein ? " (" + GCContent(GCDecimalDigits) + "% GC)" : "");
  } else if (caretPosition > -1) {
    var insertBetween = getInsertBetweenVals(caretPosition, _selectionLayer, sequenceLength);
    return "\u63D2\u5165 " + (isProtein ? "AAs " + divideBy3(insertBetween[0], true) + " and " + divideBy3(insertBetween[1] + 2, true) : "\u78B1\u57FA\u5230 " + insertBetween[0] + " \u548C " + insertBetween[1] + "\u4E4B\u95F4");
  } else {
    return "No Selection";
  }
}
export function preventDefaultStopPropagation(e) {
  if (e) {
    e.stopPropagation();
    e.preventDefault();
  }
}
export function getNodeToRefocus(caretEl) {
  var nodeToReFocus;
  if (caretEl && caretEl.closest && caretEl.closest(".veVectorInteractionWrapper")) {
    nodeToReFocus = caretEl.closest(".veVectorInteractionWrapper");
  }
  return nodeToReFocus;
}
export function getEmptyText(_ref2) {
  var sequenceData = _ref2.sequenceData,
    caretPosition = _ref2.caretPosition;
  return sequenceData.sequence.length === 0 && caretPosition === -1 ? /*#__PURE__*/React.createElement("div", {
    className: "veEmptySeqText"
  }, "Insert Sequence Here") : null;
}
export function tryToRefocusEditor() {
  var ed = document.querySelector(".veVectorInteractionWrapper");
  ed && ed.focus();
}
export function getCustomEnzymes() {
  try {
    var customEnzymes = JSON.parse(window.localStorage.getItem("customEnzymes") || "{}");
    return customEnzymes;
  } catch (error) {
    return {};
  }
}
export function addCustomEnzyme(newEnz) {
  var _extends2;
  var customEnzymes = getCustomEnzymes();
  window.localStorage.setItem("customEnzymes", JSON.stringify(_extends({}, customEnzymes, (_extends2 = {}, _extends2[newEnz.name.toLowerCase()] = newEnz, _extends2))));
}
export function pareDownAnnotations(annotations, max) {
  var annotationsToPass = annotations;
  var paredDown = false;
  if (Object.keys(annotations).length > max) {
    paredDown = true;
    var sortedAnnotations = sortBy(annotations, function (annotation) {
      return -getRangeLength(annotation);
    });
    annotationsToPass = sortedAnnotations.slice(0, max).reduce(function (obj, item) {
      obj[item.id] = item;
      return obj;
    }, {});
  }
  return [annotationsToPass, paredDown];
}
export function getParedDownWarning(_ref3) {
  var nameUpper = _ref3.nameUpper,
    maxToShow = _ref3.maxToShow,
    isAdjustable = _ref3.isAdjustable;
  return /*#__PURE__*/React.createElement(VeWarning, {
    key: "ve-warning-max" + nameUpper + "ToDisplay",
    "data-test": "ve-warning-max" + nameUpper + "ToDisplay",
    tooltip: "Warning: More than " + maxToShow + " " + (nameUpper === "Cutsites" ? "Cut Sites" : nameUpper) + ". Only displaying " + maxToShow + " " + (isAdjustable ? "(Configure this under View > Limits)" : "") + " "
  });
}
export function getClientX(event) {
  return event.clientX || get(event, "touches[0].clientX");
}
export function getClientY(event) {
  return event.clientY || get(event, "touches[0].clientY");
}
export function hideAnnByLengthFilter(hideOpts, ann, seqLen) {
  var featLength = getRangeLength(ann, seqLen);
  return hideOpts.enabled && (featLength < hideOpts.min || featLength > hideOpts.max);
}
export function getSelFromWrappedAddon(selectionLayer, sequenceLength) {
  var selToUse = _extends({}, selectionLayer);
  if (selectionLayer.isWrappedAddon) {
    var oldEnd = selToUse.end;
    selToUse.end = normalizePositionByRangeLength(selToUse.start - 1, sequenceLength);
    selToUse.start = normalizePositionByRangeLength(oldEnd + 1, sequenceLength);
    delete selToUse.isWrappedAddon;
  }
  return selToUse;
}
export function getAcceptedChars(_temp) {
  var _ref4 = _temp === void 0 ? {} : _temp,
    isOligo = _ref4.isOligo,
    isProtein = _ref4.isProtein,
    isRna = _ref4.isRna,
    isMixedRnaAndDna = _ref4.isMixedRnaAndDna;
  return isProtein ? bioData.extended_protein_letters.toLowerCase() : isOligo ? bioData.ambiguous_rna_letters.toLowerCase() + "t" : isRna ? bioData.ambiguous_rna_letters.toLowerCase() : isMixedRnaAndDna ? bioData.ambiguous_rna_letters.toLowerCase() + bioData.ambiguous_dna_letters.toLowerCase() :
  //just plain old dna
  bioData.ambiguous_dna_letters.toLowerCase();
}
export function getStripedPattern(_ref5) {
  var color = _ref5.color;
  return /*#__PURE__*/React.createElement("pattern", {
    id: "diagonalHatch",
    patternUnits: "userSpaceOnUse",
    width: "4",
    height: "4"
  }, /*#__PURE__*/React.createElement("path", {
    d: "M-1,1 l2,-2 M0,4 l4,-4 M3,5 l2,-2",
    style: {
      stroke: color,
      strokeWidth: 2,
      fill: "blue",
      opacity: 0.4
    }
  }));
}
export var getEnzymeAliases = function getEnzymeAliases(enzyme) {
  var lowerName = enzyme.name && enzyme.name.toLowerCase() || "";
  if (typeof enzyme === "string") {
    lowerName = enzyme.toLowerCase();
  }
  return filter((aliasedEnzymesByName[lowerName] || {}).aliases, function (n) {
    return n.toLowerCase() !== lowerName;
  } //filter out current enzyme
  );
};