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 { compose } from "recompose";
import React from "react";
import withEditorInteractions from "../withEditorInteractions";
import { ReactSelectField, tgFormValues } from "teselagen-react-components";
import { reduxForm } from "redux-form";
import { flatMap, forEach, keyBy } from "lodash";
import SimpleCircularOrLinearView from "../SimpleCircularOrLinearView";
import { getReverseComplementSequenceString, getSequenceDataBetweenRange, tidyUpSequenceData, shiftAnnotationsByLen } from "@teselagen/sequence-utils";
import { getRangeLength, getSequenceWithinRange } from "@teselagen/range-utils";
function PCRTool(props) {
  var sequenceData = props.sequenceData,
    _props$dimensions = props.dimensions,
    width = _props$dimensions.width,
    height = _props$dimensions.height,
    forwardPrimer = props.forwardPrimer,
    reversePrimer = props.reversePrimer,
    _primerClicked = props.primerClicked;
  var origSeqLen = sequenceData.sequence.length;
  forEach(sequenceData.primers, function (p) {
    return p.originalId = p.id;
  });
  var fPrimer = sequenceData.primers[forwardPrimer];
  var rPrimer = sequenceData.primers[reversePrimer];
  var seqBetween;
  if (fPrimer && rPrimer) {
    var r = {
      start: fPrimer.start,
      end: rPrimer.end
    };
    var newSeqData = getSequenceDataBetweenRange(sequenceData, r);
    if (rPrimer.bases) {
      // remove rPrimer length from end of seq
      var rPrimerLen = getRangeLength(rPrimer, origSeqLen);
      newSeqData.sequence = getSequenceWithinRange({
        start: 0,
        end: newSeqData.sequence.length - rPrimerLen - 1
      }, newSeqData.sequence);
      // tack back in the rPrimer.bases
      newSeqData.sequence = newSeqData.sequence + getReverseComplementSequenceString(rPrimer.bases);
      var newRPrimer = keyBy(newSeqData.primers, "id")[reversePrimer];
      newRPrimer.end = newSeqData.sequence.length - 1;
    }
    if (fPrimer.bases) {
      var fPrimerLength = getRangeLength(fPrimer, origSeqLen);
      var newSeqLen1 = newSeqData.sequence.length;
      // remove fPrimer length from start of seq
      newSeqData.sequence = getSequenceWithinRange({
        start: fPrimerLength,
        end: newSeqData.sequence.length
      }, newSeqData.sequence);
      // add back in the fPrimer.bases
      newSeqData.sequence = fPrimer.bases + newSeqData.sequence;
      // shift the existing annotations by the difference in seqLen before and after that operation
      var newSeqLen2 = newSeqData.sequence.length;
      var diff = newSeqLen2 - newSeqLen1;
      shiftAnnotationsByLen({
        seqData: newSeqData,
        insertLength: diff,
        caretPosition: 0
      });
      var newFPrimer = keyBy(newSeqData.primers, "id")[forwardPrimer];
      newFPrimer.start = 0;
    }
    seqBetween = tidyUpSequenceData(newSeqData, {
      provideNewIdsForAnnotations: true
    });
    seqBetween.name = "PCR Product from " + sequenceData.name + " ";
  }
  var getPrimers = function getPrimers(opts) {
    return flatMap(sequenceData.primers, function (_ref) {
      var name = _ref.name,
        id = _ref.id,
        forward = _ref.forward,
        start = _ref.start,
        end = _ref.end;
      if (opts.forward && !forward) return [];
      if (!opts.forward && forward) return [];
      return {
        label: /*#__PURE__*/React.createElement("div", {
          style: {
            width: "100%",
            display: "flex",
            alignItems: "center",
            justifyContent: "space-between"
          }
        }, /*#__PURE__*/React.createElement("div", null, name), /*#__PURE__*/React.createElement("div", {
          style: {
            marginLeft: 8,
            fontStyle: "italic",
            color: "grey",
            fontSize: 11
          }
        }, "(", start + 1, " - ", end + 1, ")")),
        value: id
      };
    });
  };
  var forwardPrimers = getPrimers({
    forward: true
  });
  var reversePrimers = getPrimers({
    forward: false
  });
  return /*#__PURE__*/React.createElement("div", {
    className: "createPcrTool",
    style: {
      padding: 10,
      overflowY: "auto",
      height: height
    }
  }, /*#__PURE__*/React.createElement(ReactSelectField, {
    noResultsText: "Create a new primer in the forward direction to use it in this tool",
    inlineLabel: true,
    tooltipError: true,
    options: forwardPrimers,
    defaultValue: forwardPrimers[0] ? forwardPrimers[0].value : undefined,
    name: "forwardPrimer",
    label: "\u6B63\u5411\u5F15\u7269:"
  }), /*#__PURE__*/React.createElement(ReactSelectField, {
    noResultsText: "Create a new primer in the reverse direction to use it in this tool",
    inlineLabel: true,
    tooltipError: true,
    options: reversePrimers,
    defaultValue: reversePrimers[0] ? reversePrimers[0].value : undefined,
    name: "reversePrimer",
    label: "\u53CD\u5411\u5F15\u7269:"
  }), /*#__PURE__*/React.createElement("div", {
    style: {
      fontWeight: "600",
      fontSize: 13
    }
  }, "\u4EA7\u51FA\u4EA7\u54C1:"), seqBetween ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SimpleCircularOrLinearView, {
    noWarnings: true,
    withZoomLinearView: true,
    withZoomCircularView: true,
    withChoosePreviewType: true,
    withDownload: true,
    smallSlider: true,
    withCaretEnabled: true,
    width: width - 50,
    height: Math.max(height - 250, 400),
    sequenceData: seqBetween,
    primerClicked: function primerClicked(args) {
      _primerClicked(_extends({}, args, {
        annotation: sequenceData.primers[args.annotation.originalId]
      }));
    }
  })) : /*#__PURE__*/React.createElement("div", {
    style: {
      marginTop: 5,
      fontStyle: "italic",
      color: "grey"
    }
  }, "\u8BF7\u9009\u62E9\u6B63\u5411\u548C\u53CD\u5411\u5F15\u7269\u4EE5\u67E5\u770B\u751F\u6210\u7684 PCR \u5E8F\u5217"));
}
export default compose(withEditorInteractions, reduxForm({
  form: "PCRTool"
}), tgFormValues("forwardPrimer", "reversePrimer"))(PCRTool);