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); }
import { Tooltip, AnchorButton } from "@blueprintjs/core";
import React from "react";
import { DataTable, withSelectedEntities, createCommandMenu } from "teselagen-react-components";
import getCommands from "../../commands";
import { map } from "lodash";
import { getRangeLength } from "@teselagen/range-utils";
import { connectToEditor } from "../../withEditorProps";
import { compose } from "recompose";
import selectors from "../../selectors";
import { getMassOfAaString } from "@teselagen/sequence-utils";
import { translationsSubmenu } from "../../MenuBar/viewSubmenu";
import { getVisFilter } from "./GenericAnnotationProperties";
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 = getCommands(_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 = 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: getRangeLength(translation, sequenceLength),
        sizeAa: translation.translationType === "ORF" ? Math.floor(getRangeLength(translation, sequenceLength) / 3 - 1) : Math.floor(getRangeLength(translation, sequenceLength) / 3)
      }, translation.strand === undefined && {
        strand: translation.forward ? 1 : -1
      }, {
        mass: getMassOfAaString(aaString, 2, true)
      });
    });
    return /*#__PURE__*/React.createElement("div", {
      style: {
        display: "flex",
        flexDirection: "column"
      }
    }, /*#__PURE__*/React.createElement(DataTable, {
      withCheckboxes: true,
      noPadding: true,
      onRowSelect: this.onRowSelect,
      selectedIds: selectedAnnotationId,
      formName: "translationProperties",
      noRouter: true,
      compact: true,
      topLeftItems: getVisFilter(createCommandMenu(translationsSubmenu, this.commands, {
        useTicks: true
      })),
      leftOfSearchBarItems: !readOnly && /*#__PURE__*/React.createElement(Tooltip, {
        content: translationPropertiesSelectedEntities.length && translationPropertiesSelectedEntities[0].translationType !== "User Created" ? "Only \"User Created\" translations can be deleted" : undefined
      }, /*#__PURE__*/React.createElement(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.Component);
export default compose(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.translationsSelector(editorState),
    orfs: selectors.orfsSelector(editorState),
    annotationVisibility: annotationVisibility,
    sequenceLength: (sequenceData.sequence || "").length,
    sequenceData: sequenceData
  };
}), withSelectedEntities("translationProperties"))(TranslationProperties);