"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _core = require("@blueprintjs/core");
var _react = _interopRequireDefault(require("react"));
var _teselagenReactComponents = require("teselagen-react-components");
var _commands = _interopRequireDefault(require("../../commands"));
var _lodash = require("lodash");
var _rangeUtils = require("@teselagen/range-utils");
var _withEditorProps = require("../../withEditorProps");
var _recompose = require("recompose");
var _selectors = _interopRequireDefault(require("../../selectors"));
var _sequenceUtils = require("@teselagen/sequence-utils");
var _viewSubmenu = require("../../MenuBar/viewSubmenu");
var _GenericAnnotationProperties = require("./GenericAnnotationProperties");
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 _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 TranslationProperties = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(TranslationProperties, _React$Component);
  function TranslationProperties(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    _defineProperty(_assertThisInitialized(_this), "onRowSelect", function (_ref) {
      var record = _ref[0];
      if (!record) return;
      var _this$props = _this.props,
        dispatch = _this$props.dispatch,
        editorName = _this$props.editorName;
      dispatch({
        type: "SELECTION_LAYER_UPDATE",
        payload: record,
        meta: {
          editorName: editorName
        }
      });
    });
    _this.commands = (0, _commands["default"])(_assertThisInitialized(_this));
    return _this;
  }
  var _proto = TranslationProperties.prototype;
  _proto.render = function render() {
    var _this$props2 = this.props,
      readOnly = _this$props2.readOnly,
      translations = _this$props2.translations,
      translationPropertiesSelectedEntities = _this$props2.translationPropertiesSelectedEntities,
      deleteTranslation = _this$props2.deleteTranslation,
      sequenceLength = _this$props2.sequenceLength,
      selectedAnnotationId = _this$props2.selectedAnnotationId,
      annotationVisibility = _this$props2.annotationVisibility;
    var translationsToUse = (0, _lodash.map)(translations, function (translation) {
      var aaString = "";
      for (var i = 0; i < translation.aminoAcids.length; i++) {
        aaString += translation.aminoAcids[i].aminoAcid.value;
      }
      return _extends({}, translation, {
        sizeBps: (0, _rangeUtils.getRangeLength)(translation, sequenceLength),
        sizeAa: translation.translationType === "ORF" ? Math.floor((0, _rangeUtils.getRangeLength)(translation, sequenceLength) / 3 - 1) : Math.floor((0, _rangeUtils.getRangeLength)(translation, sequenceLength) / 3)
      }, translation.strand === undefined && {
        strand: translation.forward ? 1 : -1
      }, {
        mass: (0, _sequenceUtils.getMassOfAaString)(aaString, 2, true)
      });
    });
    return /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        display: "flex",
        flexDirection: "column"
      }
    }, /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.DataTable, {
      withCheckboxes: true,
      noPadding: true,
      onRowSelect: this.onRowSelect,
      selectedIds: selectedAnnotationId,
      formName: "translationProperties",
      noRouter: true,
      compact: true,
      topLeftItems: (0, _GenericAnnotationProperties.getVisFilter)((0, _teselagenReactComponents.createCommandMenu)(_viewSubmenu.translationsSubmenu, this.commands, {
        useTicks: true
      })),
      leftOfSearchBarItems: !readOnly && /*#__PURE__*/_react["default"].createElement(_core.Tooltip, {
        content: translationPropertiesSelectedEntities.length && translationPropertiesSelectedEntities[0].translationType !== "User Created" ? "Only \"User Created\" translations can be deleted" : undefined
      }, /*#__PURE__*/_react["default"].createElement(_core.AnchorButton, {
        onClick: function onClick() {
          deleteTranslation(translationPropertiesSelectedEntities);
        },
        intent: "danger",
        icon: "trash",
        style: {
          marginLeft: 10,
          marginRight: 15,
          height: 30
        },
        disabled: !translationPropertiesSelectedEntities.length || translationPropertiesSelectedEntities[0].translationType !== "User Created"
      })),
      annotationVisibility: annotationVisibility //we need to pass this in order to force the DT to rerender
      ,
      hideSelectedCount: true,
      noFooter: true,
      noFullscreenButton: true,
      isInfinite: true,
      schema: {
        fields: [{
          path: "translationType",
          displayName: "类型",
          type: "string"
        }, {
          path: "sizeAa",
          displayName: "尺寸 (aa)",
          type: "number"
        }, {
          path: "mass",
          displayName: "质量 (g/mol)",
          type: "number"
        }, {
          path: "strand",
          displayName: "位于正/负链",
          type: "number"
        }]
      },
      entities: translationsToUse
    }));
  };
  return TranslationProperties;
}(_react["default"].Component);
var _default = (0, _recompose.compose)((0, _withEditorProps.connectToEditor)(function (editorState) {
  var readOnly = editorState.readOnly,
    _editorState$annotati = editorState.annotationVisibility,
    annotationVisibility = _editorState$annotati === void 0 ? {} : _editorState$annotati,
    sequenceData = editorState.sequenceData;
  return {
    readOnly: readOnly,
    translations: _selectors["default"].translationsSelector(editorState),
    orfs: _selectors["default"].orfsSelector(editorState),
    annotationVisibility: annotationVisibility,
    sequenceLength: (sequenceData.sequence || "").length,
    sequenceData: sequenceData
  };
}), (0, _teselagenReactComponents.withSelectedEntities)("translationProperties"))(TranslationProperties);
exports["default"] = _default;
module.exports = exports.default;