"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _recompose = require("recompose");
var _react = _interopRequireDefault(require("react"));
var _withEditorInteractions = _interopRequireDefault(require("../withEditorInteractions"));
var _teselagenReactComponents = require("teselagen-react-components");
var _reduxForm = require("redux-form");
var _lodash = require("lodash");
var _SimpleCircularOrLinearView = _interopRequireDefault(require("../SimpleCircularOrLinearView"));
var _sequenceUtils = require("@teselagen/sequence-utils");
var _rangeUtils = require("@teselagen/range-utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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 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;
  (0, _lodash.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 = (0, _sequenceUtils.getSequenceDataBetweenRange)(sequenceData, r);
    if (rPrimer.bases) {
      // remove rPrimer length from end of seq
      var rPrimerLen = (0, _rangeUtils.getRangeLength)(rPrimer, origSeqLen);
      newSeqData.sequence = (0, _rangeUtils.getSequenceWithinRange)({
        start: 0,
        end: newSeqData.sequence.length - rPrimerLen - 1
      }, newSeqData.sequence);
      // tack back in the rPrimer.bases
      newSeqData.sequence = newSeqData.sequence + (0, _sequenceUtils.getReverseComplementSequenceString)(rPrimer.bases);
      var newRPrimer = (0, _lodash.keyBy)(newSeqData.primers, "id")[reversePrimer];
      newRPrimer.end = newSeqData.sequence.length - 1;
    }
    if (fPrimer.bases) {
      var fPrimerLength = (0, _rangeUtils.getRangeLength)(fPrimer, origSeqLen);
      var newSeqLen1 = newSeqData.sequence.length;
      // remove fPrimer length from start of seq
      newSeqData.sequence = (0, _rangeUtils.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;
      (0, _sequenceUtils.shiftAnnotationsByLen)({
        seqData: newSeqData,
        insertLength: diff,
        caretPosition: 0
      });
      var newFPrimer = (0, _lodash.keyBy)(newSeqData.primers, "id")[forwardPrimer];
      newFPrimer.start = 0;
    }
    seqBetween = (0, _sequenceUtils.tidyUpSequenceData)(newSeqData, {
      provideNewIdsForAnnotations: true
    });
    seqBetween.name = "PCR Product from " + sequenceData.name + " ";
  }
  var getPrimers = function getPrimers(opts) {
    return (0, _lodash.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["default"].createElement("div", {
          style: {
            width: "100%",
            display: "flex",
            alignItems: "center",
            justifyContent: "space-between"
          }
        }, /*#__PURE__*/_react["default"].createElement("div", null, name), /*#__PURE__*/_react["default"].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["default"].createElement("div", {
    className: "createPcrTool",
    style: {
      padding: 10,
      overflowY: "auto",
      height: height
    }
  }, /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.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: "Forward Primer:"
  }), /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.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: "Reverse Primer:"
  }), /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      fontWeight: "600",
      fontSize: 13
    }
  }, "Output Product:"), seqBetween ? /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_SimpleCircularOrLinearView["default"], {
    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["default"].createElement("div", {
    style: {
      marginTop: 5,
      fontStyle: "italic",
      color: "grey"
    }
  }, "Please choose a forward and reverse primer to see the resulting PCR sequence"));
}
var _default = (0, _recompose.compose)(_withEditorInteractions["default"], (0, _reduxForm.reduxForm)({
  form: "PCRTool"
}), (0, _teselagenReactComponents.tgFormValues)("forwardPrimer", "reversePrimer"))(PCRTool);
exports["default"] = _default;
module.exports = exports.default;