"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _teselagenReactComponents = require("teselagen-react-components");
var _sequenceUtils = require("@teselagen/sequence-utils");
var _AddOrEditAnnotationDialog = _interopRequireDefault(require("../AddOrEditAnnotationDialog"));
var _rangeUtils = require("@teselagen/range-utils");
var _editorUtils = require("../../utils/editorUtils");
var _classnames = _interopRequireDefault(require("classnames"));
require("./style.css");
var _lodash = require("lodash");
var _EditCaretPosition = _interopRequireWildcard(require("./EditCaretPosition"));
var _core = require("@blueprintjs/core");
var _MeltingTemp = _interopRequireDefault(require("../../StatusBar/MeltingTemp"));
var _getStructuredBases2 = require("../../RowItem/StackedAnnotations/getStructuredBases");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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 _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 CustomContentEditable = (0, _teselagenReactComponents.generateField)(function CustomContentEditable(_ref) {
  var input = _ref.input,
    disabled = _ref.disabled,
    sequenceData = _ref.sequenceData,
    sequenceLength = _ref.sequenceLength,
    start = _ref.start,
    end = _ref.end,
    primerBindsOn = _ref.primerBindsOn,
    bases = _ref.bases,
    forward = _ref.forward;
  var _useState = (0, _react.useState)(false),
    hasTempError = _useState[0],
    setTempError = _useState[1];
  var inputRef = (0, _react.useRef)(null);
  var _useState2 = (0, _react.useState)({
      start: 0,
      end: 0
    }),
    caretPosition = _useState2[0],
    setCaretPosition = _useState2[1];
  var emitChange = function emitChange(e) {
    var newVal = e.target.innerText;
    var savedCaretPosition = _EditCaretPosition["default"].saveSelection(e.currentTarget);
    setCaretPosition(savedCaretPosition);
    var acceptedChars = (0, _editorUtils.getAcceptedChars)(sequenceData);
    var newBases = "";
    newVal.split("").forEach(function (letter) {
      if (acceptedChars.includes(letter.toLowerCase())) {
        newBases += letter;
      }
    });
    if (newVal.length !== newBases.length) {
      setTempError(true);
      setTimeout(function () {
        setTempError(false);
      }, 200);
    }
    var restore = (0, _EditCaretPosition.selectionSaveCaretPosition)(inputRef.current);
    input.onChange(newBases || "");
    setTimeout(function () {
      restore();
    }, 0);
  };
  (0, _react.useEffect)(function () {
    _EditCaretPosition["default"].restoreSelection(inputRef.current, caretPosition);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [bases]);
  var basesToUse = bases || "";
  var _getStructuredBases = (0, _getStructuredBases2.getStructuredBases)({
      annotationRange: {
        start: start - 1,
        end: end - 1
      },
      forward: forward,
      bases: basesToUse,
      start: start - 1,
      end: end - 1,
      fullSequence: sequenceData.sequence,
      primerBindsOn: primerBindsOn,
      sequenceLength: sequenceLength
    }),
    allBasesWithMetaData = _getStructuredBases.allBasesWithMetaData;
  var html = (0, _lodash.flatMap)(allBasesWithMetaData, function (_ref2) {
    var b = _ref2.b,
      isMatch = _ref2.isMatch,
      isAmbiguousMatch = _ref2.isAmbiguousMatch;
    if (b === "&") return [];
    return "<span class=\"" + (isMatch ? "" : isAmbiguousMatch ? "tg-ambiguous-match-seq" : "tg-no-match-seq") + "\">" + b + "</span>";
  });
  html = html.join("");
  return /*#__PURE__*/_react["default"].createElement("div", {
    style: _extends({
      display: "flex"
    }, disabled ? {
      pointerEvents: "none"
    } : {})
  }, /*#__PURE__*/_react["default"].createElement("span", {
    style: {
      verticalAlign: "top",
      marginTop: 9,
      marginRight: 3,
      fontSize: 12,
      color: "grey"
    }
  }, "5'"), /*#__PURE__*/_react["default"].createElement("span", {
    ref: inputRef,
    spellCheck: "false",
    contentEditable: !disabled,
    className: (0, _classnames["default"])("bp3-input tg-custom-sequence-editable", {
      hasTempError: hasTempError
    }),
    onInput: emitChange,
    dangerouslySetInnerHTML: {
      __html: html
    } // innerHTML of the editable div
  }), /*#__PURE__*/_react["default"].createElement("span", {
    style: {
      alignSelf: "end",
      marginBottom: 9,
      marginLeft: 3,
      fontSize: 12,
      color: "grey"
    }
  }, "3'"));
});
var RenderBases = function RenderBases(props) {
  var sequenceData = props.sequenceData,
    readOnly = props.readOnly,
    start = props.start,
    end = props.end,
    linkedOligo = props.linkedOligo,
    getLinkedOligoLink = props.getLinkedOligoLink,
    useLinkedOligo = props.useLinkedOligo,
    sequenceLength = props.sequenceLength,
    primerBindsOn = props.primerBindsOn,
    bases = props.bases,
    forward = props.forward,
    defaultLinkedOligoMessage = props.defaultLinkedOligoMessage,
    change = props.change;
  var defaultValue;
  var seqLen = sequenceData.sequence.length;
  var validate = (0, _react.useMemo)(function () {
    return function (val /* vals, props */) {
      if (!val) return;
      if (val.length > seqLen) {
        return "Primer cannot be longer than sequence";
      }
      return;
    };
  }, [seqLen]);
  var normalizedSelection = (0, _rangeUtils.convertRangeTo0Based)({
    start: start,
    end: end
  });
  if (!bases) {
    var bps = (0, _rangeUtils.getSequenceWithinRange)(normalizedSelection, sequenceData.sequence);
    if (!forward) {
      bps = (0, _sequenceUtils.getReverseComplementSequenceString)(bps);
    }
    defaultValue = bps;
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      borderTop: "1px solid #A7B6C2",
      borderBottom: "1px solid #A7B6C2"
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      display: "flex",
      alignItems: "center",
      justifyContent: "space-between"
    }
  }, /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.CheckboxField, {
    name: "useLinkedOligo",
    label: "\u5173\u8054Oligo?",
    tooltipInfo: "\u9009\u4E2D\u6B64\u6846\u53EF\u5C06\u6B64\u5F15\u7269\u94FE\u63A5\u5230\u5BE1\u6838\u82F7\u9178\u5E93\u4E2D\u7684\u5BE1\u6838\u82F7\u9178\u3002\u5982\u679C\u5F15\u7269\u78B1\u57FA\u4E0E\u73B0\u6709\u5BE1\u6838\u82F7\u9178\u7684\u78B1\u57FA\u5B8C\u5168\u5339\u914D,\u5219\u5B83\u5C06\u4E0E\u8BE5\u73B0\u6709\u5BE1\u6838\u82F7\u9178\u76F8\u8FDE\u3002\u5982\u679C\u78B1\u57FA\u4E0D\u5339\u914D,\u5C06\u5728\u5E93\u4E2D\u521B\u5EFA\u4E00\u4E2A\u65B0\u7684\u5BE1\u6838\u82F7\u9178\u3002",
    noMarginBottom: true,
    defaultValue: true,
    disabled: readOnly
  }), useLinkedOligo && /*#__PURE__*/_react["default"].createElement("div", {
    className: "tg-linked-oligo-holder",
    style: {
      marginTop: -5,
      fontStyle: "italic",
      fontSize: 11
    }
  }, getLinkedOligoLink && getLinkedOligoLink(props) || linkedOligo || (defaultLinkedOligoMessage !== undefined ? defaultLinkedOligoMessage : "将在保存时创建"))), useLinkedOligo && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(CustomContentEditable
  // inlineLabel
  , _extends({
    showErrorIfUntouched: true,
    tooltipError: true,
    primerBindsOn: primerBindsOn,
    validate: validate,
    sequenceLength: sequenceLength,
    disabled: readOnly
  }, props, defaultValue ? {
    defaultValue: defaultValue
  } : {}, {
    name: "bases",
    label: /*#__PURE__*/_react["default"].createElement("div", {
      className: "tg-bases-label"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        display: "flex"
      }
    }, "\u5E8F\u5217", " ", /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        fontSize: 10
      }
    }, " ", "\xA0 (\u957F\u5EA6: ", bases ? bases.length : 0, ")")), /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        width: "fit-content"
      }
    }, /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.DropdownButton, {
      disabled: readOnly,
      intent: "primary",
      small: true,
      menu: /*#__PURE__*/_react["default"].createElement(_core.Menu, null, /*#__PURE__*/_react["default"].createElement(_core.MenuItem, {
        onClick: function onClick() {
          change("forward", true);
          change("bases", (0, _rangeUtils.getSequenceWithinRange)(normalizedSelection, sequenceData.sequence));
        },
        key: "forward",
        text: "\u6B63\u5411"
      }), /*#__PURE__*/_react["default"].createElement(_core.MenuItem, {
        onClick: function onClick() {
          change("forward", false);
          change("bases", (0, _sequenceUtils.getReverseComplementSequenceString)((0, _rangeUtils.getSequenceWithinRange)(normalizedSelection, sequenceData.sequence)));
        },
        key: "reverse",
        text: "\u53CD\u5411"
      }))
    }, "\u76F4\u63A5\u5F15\u7528\u9009\u533A")))
  })), /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.AdvancedOptions, {
    label: '高级设置',
    style: {
      marginBottom: 10
    }
  }, /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.RadioGroupField, {
    name: "primerBindsOn",
    inline: true,
    inlineLabel: true,
    disabled: readOnly,
    label: "\u914D\u5BF9\u533A\u57DF",
    tooltipError: true,
    options: [{
      label: "5' 端",
      value: "5prime"
    }, {
      label: "3' 端",
      value: "3prime"
    }]
  }))));
};
var _default = (0, _AddOrEditAnnotationDialog["default"])({
  formName: "AddOrEditPrimerDialog",
  getProps: function getProps(props) {
    return {
      upsertAnnotation: props.upsertPrimer,
      annotationTypePlural: "primers",
      RenderBases: RenderBases
    };
  }
});
exports["default"] = _default;
var InnerWrapperMeltingTemp = function InnerWrapperMeltingTemp(p) {
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "bp3-text-muted bp3-text-small",
    style: {
      marginBottom: 15,
      marginTop: -5,
      fontStyle: "italic"
    }
  }, p.children);
};
module.exports = exports.default;