"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _reactRedux = require("react-redux");
var _react = _interopRequireDefault(require("react"));
var _teselagenReactComponents = require("teselagen-react-components");
var _sequenceUtils = require("@teselagen/sequence-utils");
var _EnzymeViewer = _interopRequireDefault(require("../EnzymeViewer"));
require("./style.css");
var _withEditorProps = require("../withEditorProps");
var _recompose = require("recompose");
var _core = require("@blueprintjs/core");
var _editorUtils = require("../utils/editorUtils");
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); }
var CreateCustomEnzyme = function CreateCustomEnzyme(props) {
  var paddingStart = "-------";
  var paddingEnd = "-------";
  var _props$inputSequenceT = props.inputSequenceToTestAgainst,
    inputSequenceToTestAgainst = _props$inputSequenceT === void 0 ? "" : _props$inputSequenceT,
    _props$seqName = props.seqName,
    seqName = _props$seqName === void 0 ? "Destination Vector" : _props$seqName,
    createYourOwnEnzyme = props.createYourOwnEnzyme,
    dispatch = props.dispatch,
    hideModal = props.hideModal,
    editorName = props.editorName;
  createYourOwnEnzyme.chop_top_index = Number(createYourOwnEnzyme.chop_top_index);
  createYourOwnEnzyme.chop_bottom_index = Number(createYourOwnEnzyme.chop_bottom_index);
  var _createYourOwnEnzyme$ = createYourOwnEnzyme.sequence,
    sequence = _createYourOwnEnzyme$ === void 0 ? "" : _createYourOwnEnzyme$,
    _createYourOwnEnzyme$2 = createYourOwnEnzyme.chop_top_index,
    chop_top_index = _createYourOwnEnzyme$2 === void 0 ? 0 : _createYourOwnEnzyme$2,
    _createYourOwnEnzyme$3 = createYourOwnEnzyme.chop_bottom_index,
    chop_bottom_index = _createYourOwnEnzyme$3 === void 0 ? 0 : _createYourOwnEnzyme$3,
    _createYourOwnEnzyme$4 = createYourOwnEnzyme.name,
    name = _createYourOwnEnzyme$4 === void 0 ? "" : _createYourOwnEnzyme$4;
  var regexString = bpsToRegexString(sequence);
  var enzyme = {
    name: name,
    site: sequence,
    forwardRegex: regexString,
    reverseRegex: (0, _sequenceUtils.getReverseComplementSequenceString)(regexString),
    topSnipOffset: chop_top_index,
    bottomSnipOffset: chop_bottom_index,
    usForward: 0,
    usReverse: 0,
    color: "black"
  };
  var invalid;
  if (!enzyme.name || !enzyme.site || !enzyme.forwardRegex || !enzyme.reverseRegex || !enzyme.topSnipOffset && enzyme.topSnipOffset !== 0 || !enzyme.bottomSnipOffset && enzyme.bottomSnipOffset !== 0) {
    invalid = true;
  }
  var matches;
  if (regexString.length === 0) {
    matches = [];
  } else {
    matches = (0, _sequenceUtils.cutSequenceByRestrictionEnzyme)(inputSequenceToTestAgainst, true, enzyme);
  }
  var errors = validate(createYourOwnEnzyme);
  if (Object.keys(errors || {}).length) {
    invalid = true;
  }
  function onChange(updatedVal) {
    dispatch({
      type: "CREATE_YOUR_OWN_ENZYME_UPDATE",
      payload: _extends({}, createYourOwnEnzyme, updatedVal)
    });
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "createYourOwnEnzyme"
  }, /*#__PURE__*/_react["default"].createElement(_core.Callout, {
    intent: "primary"
  }, "\u8BE5\u9176\u5C06\u81EA\u52A8\u52A0\u5165\"\u6211\u7684\u9176\"\u5206\u7EC4"), /*#__PURE__*/_react["default"].createElement(CustomInput, {
    error: errors["name"],
    value: name,
    onChange: onChange,
    name: "name",
    label: "\u540D\u79F0:"
  }), /*#__PURE__*/_react["default"].createElement(CustomInput, {
    error: errors["sequence"],
    value: sequence,
    onChange: onChange,
    name: "sequence",
    label: /*#__PURE__*/_react["default"].createElement("div", {
      className: "labelWithIcon"
    }, /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.InfoHelper, null, /*#__PURE__*/_react["default"].createElement("div", {
      className: "taLineHolder"
    }, /*#__PURE__*/_react["default"].createElement(Line, null, " Special Characters: "), /*#__PURE__*/_react["default"].createElement(Line, null, " R = G A (purine) "), /*#__PURE__*/_react["default"].createElement(Line, null, " Y = T C (pyrimidine) "), /*#__PURE__*/_react["default"].createElement(Line, null, " K = G T (keto) "), /*#__PURE__*/_react["default"].createElement(Line, null, " M = A C (amino) "), /*#__PURE__*/_react["default"].createElement(Line, null, " S = G C (strong bonds) "), /*#__PURE__*/_react["default"].createElement(Line, null, " W = A T (weak bonds) "), /*#__PURE__*/_react["default"].createElement(Line, null, " B = G T C (all but A) "), /*#__PURE__*/_react["default"].createElement(Line, null, " D = G A T (all but C) "), /*#__PURE__*/_react["default"].createElement(Line, null, " H = A C T (all but G) "), /*#__PURE__*/_react["default"].createElement(Line, null, " V = G C A (all but T) "), /*#__PURE__*/_react["default"].createElement(Line, null, " N = A G C T (any) "))), /*#__PURE__*/_react["default"].createElement("span", null, "\u8BC6\u522B\u4F4D\u70B9:")),
    onInput: function onInput(input) {
      var inputValue = input.target.value;
      var cleanInput = inputValue.replace(/[^rykmswbdhvnagct]/gi, "");
      input.target.value = cleanInput;
    }
  }), /*#__PURE__*/_react["default"].createElement(CustomInput, {
    error: errors["chop_top_index"],
    value: chop_top_index,
    onChange: onChange,
    name: "chop_top_index",
    label: "\u4E0A\u94FE\u5207\u53E3\u4F4D\u7F6E:",
    type: "number"
  }), /*#__PURE__*/_react["default"].createElement(CustomInput, {
    error: errors["chop_bottom_index"],
    value: chop_bottom_index,
    onChange: onChange,
    name: "chop_bottom_index",
    label: "\u4E0B\u94FE\u5207\u53E3\u4F4D\u7F6E:",
    type: "number"
  }), /*#__PURE__*/_react["default"].createElement(_EnzymeViewer["default"], {
    forwardSnipPosition: chop_top_index,
    paddingEnd: paddingEnd,
    paddingStart: paddingStart,
    reverseSnipPosition: chop_bottom_index,
    sequence: sequence
  }), /*#__PURE__*/_react["default"].createElement("br", null), /*#__PURE__*/_react["default"].createElement("h3", {
    className: "cutnumber " + (matches.length === 0 && "invalid")
  }, matches.length > 10 ? "\u5728" + seqName + "\u4E2D\u627E\u5230\u8D85\u8FC710\u4E2A\u4F4D\u70B9" : "\u5728" + seqName + "\u4E2D\u627E\u5230" + matches.length + "\u4E2A\u4F4D\u70B9"), /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.DialogFooter, {
    hideModal: hideModal,
    disabled: invalid,
    onClick: function onClick() {
      (0, _editorUtils.addCustomEnzyme)(enzyme);
      dispatch({
        type: "FILTERED_RESTRICTION_ENZYMES_ADD",
        //filteredRestrictionEnzymesAdd
        payload: {
          value: name
        },
        meta: {
          editorName: editorName
        }
      });
      hideModal && hideModal();
    }
  }));
};
var _default = (0, _recompose.compose)((0, _teselagenReactComponents.wrapDialog)({
  title: "新建内切酶"
}), (0, _withEditorProps.connectToEditor)(function (_ref) {
  var _ref$sequenceData = _ref.sequenceData,
    sequenceData = _ref$sequenceData === void 0 ? {} : _ref$sequenceData;
  return {
    seqName: sequenceData.name,
    inputSequenceToTestAgainst: sequenceData.sequence || ""
  };
}), (0, _reactRedux.connect)(function (state) {
  return {
    createYourOwnEnzyme: state.VectorEditor.__allEditorsOptions.createYourOwnEnzyme
  };
}))(CreateCustomEnzyme);
exports["default"] = _default;
function validate(values) {
  var errors = {};
  if (!values.name || values.name.trim() === "") {
    errors.name = "Input cannot be blank";
  } else if (_sequenceUtils.defaultEnzymesByName[values.name.toLowerCase()]) {
    errors.name = "The name " + values.name + " is already taken.";
  }
  if (!values.sequence || values.sequence.trim() === "" || values.sequence.trim().length < 4) {
    errors.sequence = "Enzyme recognition sequence must be at least 4bps long";
  }
  if (values.sequence && values.sequence.replace(/[^atgcrykmswbdhvn]/gi, "").length !== values.sequence.length) {
    errors.sequence = "Sequence must only contain valid bases";
  }
  if (!values.chop_top_index && values.chop_top_index !== 0) {
    errors.chop_top_index = "Input cannot be blank";
  }
  if (!values.chop_bottom_index && values.chop_bottom_index !== 0) {
    errors.chop_bottom_index = "Input cannot be blank";
  }
  return errors;
}

// connect(
//   function(state, props) {
//     const editorState = state.VectorEditor[props.editorName];
//     let selectedEnzymes = s.restrictionEnzymesSelector(editorState, props.additionalEnzymes);
//     return {
//       selectedEnzymes
//     };
//   },
//   { createYourOwnEnzymeClose }
// )(ManageEnzymes);

// export default ManageEnzymes;

function bpsToRegexString(bps) {
  var regexString = "";
  if (typeof bps === "string") {
    bps.split("").forEach(function (bp) {
      if (bp === "r") {
        regexString += "[ga]";
      } else if (bp === "y") {
        regexString += "[tc]";
      } else if (bp === "k") {
        regexString += "[gt]";
      } else if (bp === "m") {
        regexString += "[ac]";
      } else if (bp === "s") {
        regexString += "[gc]";
      } else if (bp === "w") {
        regexString += "[at]";
      } else if (bp === "b") {
        regexString += "[gtc]";
      } else if (bp === "d") {
        regexString += "[gat]";
      } else if (bp === "h") {
        regexString += "[act]";
      } else if (bp === "v") {
        regexString += "[gca]";
      } else if (bp === "n") {
        regexString += "[agct]";
      } else {
        regexString += bp;
      }
    });
  }
  return regexString;
}
function CustomInput(_ref2) {
  var name = _ref2.name,
    value = _ref2.value,
    _onChange2 = _ref2.onChange,
    onInput = _ref2.onInput,
    label = _ref2.label,
    error = _ref2.error,
    type = _ref2.type;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "inputHolder " + (error && "error")
  }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("span", null, label), /*#__PURE__*/_react["default"].createElement("input", {
    value: value,
    onChange: function onChange(e) {
      var _onChange;
      _onChange2((_onChange = {}, _onChange[name] = e.target.value, _onChange));
    },
    onInput: onInput,
    type: type
  })), error && /*#__PURE__*/_react["default"].createElement("p", {
    className: "errorMessage"
  }, error));
}
function Line(_ref3) {
  var children = _ref3.children;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "taLine"
  }, " ", children);
}
module.exports = exports.default;