"use strict";

exports.__esModule = true;
exports["default"] = exports.DigestTool = void 0;
var _recompose = require("recompose");
var _react = _interopRequireDefault(require("react"));
var _teselagenReactComponents = require("teselagen-react-components");
var _sequenceUtils = require("@teselagen/sequence-utils");
var _CutsiteFilter = _interopRequireDefault(require("../CutsiteFilter"));
var _Ladder = _interopRequireDefault(require("./Ladder"));
var _core = require("@blueprintjs/core");
var _withEditorInteractions = _interopRequireDefault(require("../withEditorInteractions"));
var _userDefinedHandlersAndOpts = require("../Editor/userDefinedHandlersAndOpts");
var _lodash = require("lodash");
var _excluded = ["id", "cut1", "cut2", "start", "end", "size"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var MAX_DIGEST_CUTSITES = 50;
var MAX_PARTIAL_DIGEST_CUTSITES = 10;
var DigestTool = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(DigestTool, _React$Component);
  function DigestTool() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _defineProperty(_assertThisInitialized(_this), "state", {
      selectedTab: "virtualDigest"
    });
    return _this;
  }
  var _proto = DigestTool.prototype;
  _proto.render = function render() {
    var _this2 = this,
      _value;
    var _this$props = this.props,
      editorName = _this$props.editorName,
      _this$props$dimension = _this$props.dimensions,
      dimensions = _this$props$dimension === void 0 ? {} : _this$props$dimension,
      lanes = _this$props.lanes,
      _this$props$digestToo = _this$props.digestTool,
      selectedFragment = _this$props$digestToo.selectedFragment,
      computePartialDigest = _this$props$digestToo.computePartialDigest,
      onDigestSave = _this$props.onDigestSave,
      computePartialDigestDisabled = _this$props.computePartialDigestDisabled,
      computeDigestDisabled = _this$props.computeDigestDisabled,
      updateComputePartialDigest = _this$props.updateComputePartialDigest;
    var selectedTab = this.state.selectedTab;
    return /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        height: typeof dimensions.height === "string" ? 100 : dimensions.height,
        overflowY: "auto",
        padding: 10
      }
    }, onDigestSave && /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        display: "flex",
        marginBottom: 10
      }
    }, /*#__PURE__*/_react["default"].createElement(_core.InputGroup, {
      placeholder: "My Digest"
    }), /*#__PURE__*/_react["default"].createElement(_core.Button, {
      intent: _core.Intent.PRIMARY,
      onClick: function onClick() {
        onDigestSave({});
      },
      style: {
        marginLeft: 5
      }
    }, " ", "Save")), /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        display: "flex",
        justifyContent: "space-between"
      }
    }, /*#__PURE__*/_react["default"].createElement(_core.Checkbox, {
      onChange: function onChange() {
        updateComputePartialDigest(!computePartialDigest);
      },
      checked: computePartialDigest,
      label: /*#__PURE__*/_react["default"].createElement("span", null, "\u663E\u793A\u90E8\u5206\u9176\u5207\u65B9\u6848", " ", computePartialDigestDisabled ? /*#__PURE__*/_react["default"].createElement("span", {
        style: {
          fontSize: 10
        }
      }, " ", "-- \u7981\u7528 (\u53EA\u652F\u6301 ", MAX_PARTIAL_DIGEST_CUTSITES, " \u6216 \u66F4\u5C11\u7684\u526A\u5207\u4F4D\u70B9)", " ") : null),
      disabled: computePartialDigestDisabled
    }), /*#__PURE__*/_react["default"].createElement("a", {
      target: "_blank",
      rel: "noopener noreferrer",
      href: "http://docs.teselagen.com/en/articles/5489322-restriction-digest-in-the-vector-editor"
    }, "\u9605\u8BFB\u6587\u6863")), "\u9009\u62E9\u60A8\u7684\u5185\u5207\u9176:", /*#__PURE__*/_react["default"].createElement(_CutsiteFilter["default"], _extends({}, (0, _lodash.pick)(this.props, _userDefinedHandlersAndOpts.userDefinedHandlersAndOpts), {
      editorName: editorName
    })), /*#__PURE__*/_react["default"].createElement("br", null), computeDigestDisabled && /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        color: "red",
        marginBottom: "6px",
        fontSize: "15px"
      }
    }, ">" + MAX_DIGEST_CUTSITES + " \u68C0\u6D4B\u5230\u5207\u5272\u4F4D\u70B9\u3002\u8FC7\u6EE4\u6389\u989D\u5916\u7684\n                \u9650\u5236\u6027\u5185\u5207\u9176\u53EF\u89C6\u5316\u6D88\u5316\u7ED3\u679C"), /*#__PURE__*/_react["default"].createElement(_core.Tabs, {
      selectedTabId: selectedTab,
      onChange: function onChange(id) {
        _this2.setState({
          selectedTab: id
        });
      }
    }, /*#__PURE__*/_react["default"].createElement(_core.Tab, {
      title: "\u6A21\u62DF\u9176\u5207",
      id: "virtualDigest",
      panel: /*#__PURE__*/_react["default"].createElement(_Ladder["default"], _extends({}, this.props, {
        editorName: editorName
      }))
    }), /*#__PURE__*/_react["default"].createElement(_core.Tab, {
      title: "\u6458\u8981\u4FE1\u606F",
      id: "table",
      panel: /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.DataTable, {
        noRouter: true,
        isSimple: true,
        maxHeight: 400
        // noFooter
        ,
        withSearch: false,
        onSingleRowSelect: function onSingleRowSelect(_ref) {
          var onFragmentSelect = _ref.onFragmentSelect;
          onFragmentSelect();
        },
        reduxFormSelectedEntityIdMap: {
          input: {
            value: (_value = {}, _value[selectedFragment] = true, _value),
            onChange: function onChange() {}
          }
        },
        formName: "digestInfoTable",
        entities: lanes[0].map(function (_ref2) {
          var id = _ref2.id,
            cut1 = _ref2.cut1,
            cut2 = _ref2.cut2,
            start = _ref2.start,
            end = _ref2.end,
            size = _ref2.size,
            rest = _objectWithoutPropertiesLoose(_ref2, _excluded);
          return _extends({}, rest, {
            id: id,
            start: start,
            end: end,
            length: size,
            leftCutter: cut1.restrictionEnzyme.name,
            rightCutter: cut2.restrictionEnzyme.name,
            leftOverhang: (0, _sequenceUtils.getCutsiteType)(cut1.restrictionEnzyme),
            rightOverhang: (0, _sequenceUtils.getCutsiteType)(cut2.restrictionEnzyme)
          });
        }),
        schema: schema
      })
    })), /*#__PURE__*/_react["default"].createElement("br", null));
  };
  return DigestTool;
}(_react["default"].Component);
exports.DigestTool = DigestTool;
var schema = {
  fields: [{
    width: 60,
    path: "start",
    displayName: "开始",
    type: "string"
  }, {
    width: 60,
    path: "end",
    displayName: "结束",
    type: "string"
  }, {
    width: 70,
    path: "length",
    displayName: "长度",
    type: "string"
  }, {
    path: "leftCutter",
    displayName: "左切",
    type: "string"
  }, {
    path: "leftOverhang",
    displayName: "左剩",
    type: "string"
  }, {
    path: "rightCutter",
    displayName: "右切",
    type: "string"
  }, {
    path: "rightOverhang",
    displayName: "右剩",
    type: "string"
  }]
};
var _default = (0, _recompose.compose)(_withEditorInteractions["default"], (0, _recompose.withProps)(function (props) {
  var sequenceData = props.sequenceData,
    sequenceLength = props.sequenceLength,
    selectionLayerUpdate = props.selectionLayerUpdate,
    updateSelectedFragment = props.updateSelectedFragment,
    computePartialDigest = props.digestTool.computePartialDigest;
  var isCircular = sequenceData.circular;
  var cutsites = sequenceData.cutsites;
  var computePartialDigestDisabled = cutsites.length > MAX_PARTIAL_DIGEST_CUTSITES;
  var computeDigestDisabled = cutsites.length > MAX_DIGEST_CUTSITES;
  var _getVirtualDigest = (0, _sequenceUtils.getVirtualDigest)({
      cutsites: cutsites,
      sequenceLength: sequenceLength,
      isCircular: isCircular,
      computePartialDigest: computePartialDigest,
      computePartialDigestDisabled: computePartialDigestDisabled,
      computeDigestDisabled: computeDigestDisabled
    }),
    fragments = _getVirtualDigest.fragments,
    overlappingEnzymes = _getVirtualDigest.overlappingEnzymes;
  return {
    computePartialDigestDisabled: computePartialDigestDisabled,
    computeDigestDisabled: computeDigestDisabled,
    lanes: [fragments.map(function (f) {
      return _extends({}, f, {
        onFragmentSelect: function onFragmentSelect() {
          selectionLayerUpdate({
            start: f.start,
            end: f.end,
            name: f.name
          });
          updateSelectedFragment(f.Intentid);
        }
      });
    })],
    overlappingEnzymes: overlappingEnzymes
  };
}))(DigestTool);
exports["default"] = _default;