"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _teselagenReactComponents = require("teselagen-react-components");
var _CircularView = require("./CircularView");
var _LinearView = require("./LinearView");
var _RowView = require("./RowView");
var _withHover = require("./helperComponents/withHover");
var _annotationVisibility2 = require("./redux/annotationVisibility");
var _addWrappedAddons = require("./utils/addWrappedAddons");
var _SimpleOligoPreview = require("./SimpleOligoPreview");
var _lodash = require("lodash");
var _core = require("@blueprintjs/core");
var _commands = _interopRequireDefault(require("./commands"));
var _recompose = require("recompose");
var _withEditorProps = require("./withEditorProps");
var _clickAndDragUtils = require("./withEditorInteractions/clickAndDragUtils");
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); }
//this view is meant to be a helper for showing a simple (non-redux connected) circular or linear view!
var _default = function _default(props) {
  var _sequenceData = props.sequenceData,
    _props$annotationVisi = props.annotationVisibility,
    _annotationVisibility = _props$annotationVisi === void 0 ? {} : _props$annotationVisi,
    _props$noWarnings = props.noWarnings,
    noWarnings = _props$noWarnings === void 0 ? true : _props$noWarnings,
    withDownload = props.withDownload,
    withChoosePreviewType = props.withChoosePreviewType,
    withCaretEnabled = props.withCaretEnabled,
    withSelectionEnabled = props.withSelectionEnabled,
    smallSlider = props.smallSlider,
    withVisibilityOptions = props.withVisibilityOptions,
    minimalPreviewTypeBtns = props.minimalPreviewTypeBtns,
    withFullscreen = props.withFullscreen,
    selectionLayer = props.selectionLayer,
    selectionLayerUpdate = props.selectionLayerUpdate,
    caretPositionUpdate = props.caretPositionUpdate;
  var _useState = (0, _react.useState)(_sequenceData.circular ? "circular" : "linear"),
    previewType = _useState[0],
    setPreviewType = _useState[1];
  var _useState2 = (0, _react.useState)(false),
    isPopoverOpen = _useState2[0],
    setPopoverOpen = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    isFullscreen = _useState3[0],
    setFullscreen = _useState3[1];
  var _useState4 = (0, _react.useState)({}),
    visibilityOptions = _useState4[0],
    setVisibilityOptions = _useState4[1];
  var _useState5 = (0, _react.useState)(withCaretEnabled ? -1 : undefined),
    caretPosition = _useState5[0],
    _caretPositionUpdate = _useState5[1];
  var _useState6 = (0, _react.useState)(withSelectionEnabled ? {
      start: -1,
      end: -1
    } : undefined),
    _selectionLayer = _useState6[0],
    _selectionLayerUpdate = _useState6[1];
  var sequenceLength = _sequenceData.noSequence ? _sequenceData.size : _sequenceData.sequence.length;
  selectionLayer = selectionLayer || _selectionLayer;
  var selectionLayerUpdateOld = selectionLayerUpdate || _selectionLayerUpdate;
  var caretPositionUpdateOld = caretPositionUpdate || _caretPositionUpdate;
  selectionLayerUpdate = function selectionLayerUpdate(newSel, dontTrigger) {
    if (!dontTrigger && newSel.start > -1) {
      caretPositionUpdate(-1, true);
    }
    selectionLayerUpdateOld(newSel);
  };
  caretPositionUpdate = function caretPositionUpdate(newCaret, dontTrigger) {
    if (!dontTrigger && newCaret > -1) {
      selectionLayerUpdate({
        start: -1,
        end: -1
      }, true);
    }
    caretPositionUpdateOld(newCaret);
  };
  function annotationClicked(_ref) {
    var annotation = _ref.annotation,
      event = _ref.event;
    event.stopPropagation();
    event.preventDefault();
    withSelectionEnabled && (0, _clickAndDragUtils.updateSelectionOrCaret)({
      doNotWrapOrigin: !sequenceData.circular,
      shiftHeld: event.shiftKey,
      sequenceLength: sequenceLength,
      newRangeOrCaret: annotation,
      caretPosition: caretPosition,
      selectionLayer: selectionLayer,
      selectionLayerUpdate: selectionLayerUpdate,
      caretPositionUpdate: caretPositionUpdate
    });
  }
  var tickSpacing = undefined;
  var Component = (withChoosePreviewType ? previewType === "circular" : _sequenceData.circular) ? _CircularView.CircularView : _sequenceData.isOligo && _sequenceData.sequence ? _SimpleOligoPreview.SimpleOligoPreview : _LinearView.LinearView;
  if (withChoosePreviewType && previewType === "row") {
    Component = _RowView.RowView;
    tickSpacing = undefined;
  }
  var sequenceData = (0, _lodash.cloneDeep)(_sequenceData);
  var annotationVisibility = _extends({}, _annotationVisibility2.visibilityDefaultValues, _annotationVisibility, visibilityOptions);

  //here we're making it possible to not pass a sequenceData.sequence
  //we can just pass a .size property to save having to send the whole sequence if it isn't needed!
  if (sequenceData.noSequence) {
    annotationVisibility.sequence = false;
    annotationVisibility.reverseSequence = false;
    if (sequenceData.size === undefined) {
      return /*#__PURE__*/_react["default"].createElement("div", null, "Error: No sequenceData.size detected when using noSequence flag", " ");
    }
    sequenceData = _extends({}, sequenceData, {
      sequence: {
        length: sequenceData.size
      }
    });
  }
  sequenceData.parts = (0, _addWrappedAddons.addWrappedAddons)(sequenceData.parts, sequenceData.sequence.length);
  var inner = function inner(_ref2) {
    var width = _ref2.width,
      height = _ref2.height;
    return /*#__PURE__*/_react["default"].createElement(_withHover.HoveredIdContext.Provider, {
      value: {
        hoveredId: props.hoveredId
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        width: "fit-content"
      }
    }, (withDownload || withChoosePreviewType || withFullscreen || VisibilityOptions) && /*#__PURE__*/_react["default"].createElement("div", {
      style: _extends({
        marginLeft: 10,
        marginBottom: 5
      }, isFullscreen && {
        marginRight: 10,
        paddingTop: 10
      }, {
        display: "flex",
        justifyContent: "end"
      })
    }, withDownload && /*#__PURE__*/_react["default"].createElement(DownloadBtn, props), withVisibilityOptions && /*#__PURE__*/_react["default"].createElement(VisibilityOptions, _extends({}, props, {
      sequenceData: sequenceData,
      annotationVisibility: annotationVisibility,
      setVisibilityOptions: setVisibilityOptions,
      isPopoverOpen: isPopoverOpen,
      setPopoverOpen: setPopoverOpen
    })), withChoosePreviewType && /*#__PURE__*/_react["default"].createElement(_core.ButtonGroup, null, /*#__PURE__*/_react["default"].createElement(_core.Tooltip, {
      content: "Circular View"
    }, /*#__PURE__*/_react["default"].createElement(_core.Button, {
      minimal: minimalPreviewTypeBtns,
      className: "tgPreviewTypeCircular",
      active: previewType === "circular",
      intent: "primary",
      onClick: function onClick() {
        return setPreviewType("circular");
      },
      icon: "circle"
    })), /*#__PURE__*/_react["default"].createElement(_core.Tooltip, {
      content: "Linear View"
    }, /*#__PURE__*/_react["default"].createElement(_core.Button, {
      minimal: minimalPreviewTypeBtns,
      className: "tgPreviewTypeLinear",
      active: previewType === "linear",
      intent: "primary",
      onClick: function onClick() {
        return setPreviewType("linear");
      },
      icon: "layout-linear"
    })), /*#__PURE__*/_react["default"].createElement(_core.Tooltip, {
      content: "Sequence View"
    }, /*#__PURE__*/_react["default"].createElement(_core.Button, {
      minimal: minimalPreviewTypeBtns,
      className: "tgPreviewTypeRow",
      active: previewType === "row",
      intent: "primary",
      onClick: function onClick() {
        return setPreviewType("row");
      },
      icon: "menu"
    }))), withFullscreen && /*#__PURE__*/_react["default"].createElement(FullscreenBtn, {
      setFullscreen: setFullscreen,
      isFullscreen: isFullscreen
    })), /*#__PURE__*/_react["default"].createElement(Component, _extends({
      showCicularViewInternalLabels: true,
      className: "tg-simple-dna-view veEditor",
      width: 300,
      height: 300,
      noWarnings: noWarnings,
      partClicked: annotationClicked,
      featureClicked: annotationClicked,
      primerClicked: annotationClicked
    }, props, isFullscreen && {
      width: width - 10,
      height: height - 10
    }, {
      smartCircViewLabelRender: true,
      caretPosition: caretPosition,
      smallSlider: smallSlider
    }, withSelectionEnabled && {
      selectionLayer: selectionLayer,
      selectionLayerUpdate: selectionLayerUpdate
    }, {
      readOnly: true,
      editorClicked: function editorClicked(_temp) {
        var _ref3 = _temp === void 0 ? {} : _temp,
          nearestCaretPos = _ref3.nearestCaretPos,
          event = _ref3.event;
        if (!withCaretEnabled) {
          if (!withSelectionEnabled) return;
          if (!event.shiftKey) return;
          if (!(selectionLayer.start > -1)) return;
        }
        (0, _clickAndDragUtils.editorClicked)({
          nearestCaretPos: nearestCaretPos,
          shiftHeld: !withSelectionEnabled ? false : event.shiftKey,
          updateSelectionOrCaret: function updateSelectionOrCaret(shiftHeld, newRangeOrCaret) {
            (0, _clickAndDragUtils.updateSelectionOrCaret)({
              doNotWrapOrigin: !sequenceData.circular,
              sequenceLength: sequenceLength,
              shiftHeld: shiftHeld,
              newRangeOrCaret: newRangeOrCaret,
              caretPosition: caretPosition,
              selectionLayer: selectionLayer,
              selectionLayerUpdate: selectionLayerUpdate,
              caretPositionUpdate: caretPositionUpdate
            });
          }
        });
      },
      instantiated: true,
      tickSpacing: tickSpacing,
      hoveredId: props.hoveredId,
      annotationVisibility: annotationVisibility,
      sequenceData: sequenceData,
      showTitle: true
    }))));
  };
  if (isFullscreen) {
    return /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.FillWindow, {
      asPortal: true,
      className: "tgSimpleViewFullscreen"
    }, inner);
  }
  return inner({});
};
exports["default"] = _default;
var DownloadBtn = (0, _recompose.withHandlers)({
  exportSequenceToFile: _withEditorProps.exportSequenceToFile
})(function (props) {
  return /*#__PURE__*/_react["default"].createElement(_core.Tooltip, {
    content: "Download"
  }, /*#__PURE__*/_react["default"].createElement(_core.Button, {
    className: "veDownloadButton",
    style: {
      marginRight: 10
    },
    onClick: function onClick(event) {
      return (0, _teselagenReactComponents.showContextMenu)(["exportSequenceAsGenbank", "exportSequenceAsFasta", "exportSequenceAsTeselagenJson"], [(0, _teselagenReactComponents.commandMenuEnhancer)((0, _commands["default"])({
        props: props
      }), {
        useTicks: true,
        omitIcons: true
      })], event);
    },
    minimal: true,
    intent: "primary",
    icon: "download"
  }));
});
var FullscreenBtn = function FullscreenBtn(_ref4) {
  var setFullscreen = _ref4.setFullscreen,
    isFullscreen = _ref4.isFullscreen;
  return /*#__PURE__*/_react["default"].createElement(_core.Tooltip, {
    content: (isFullscreen ? "Close " : "") + "Fullscreen"
  }, /*#__PURE__*/_react["default"].createElement(_core.Button, {
    className: "veFullscreenButton",
    style: {
      marginLeft: 10,
      marginRight: 10
    },
    onClick: function onClick() {
      setFullscreen(!isFullscreen);
    },
    minimal: true,
    intent: "primary",
    icon: !isFullscreen ? "maximize" : "minimize"
  }));
};
var VisibilityOptions = function VisibilityOptions(_ref5) {
  var annotationVisibility = _ref5.annotationVisibility,
    sequenceData = _ref5.sequenceData,
    setVisibilityOptions = _ref5.setVisibilityOptions,
    isPopoverOpen = _ref5.isPopoverOpen,
    setPopoverOpen = _ref5.setPopoverOpen;
  return /*#__PURE__*/_react["default"].createElement(_core.Tooltip, {
    disabled: isPopoverOpen,
    content: "Visibility Options"
  }, /*#__PURE__*/_react["default"].createElement(_core.Popover, {
    minimal: true,
    onInteraction: function onInteraction(isOpen) {
      setPopoverOpen(isOpen);
    },
    isOpen: isPopoverOpen,
    content: /*#__PURE__*/_react["default"].createElement(_core.Menu, null, (0, _lodash.flatMap)(["features", "parts", "primers", "translations", "cutsites"], function (name) {
      if (!(0, _lodash.map)(sequenceData[name]).length) return [];
      return /*#__PURE__*/_react["default"].createElement(_core.MenuItem, {
        onClick: function onClick(e) {
          var _extends2;
          setVisibilityOptions(_extends({}, annotationVisibility, (_extends2 = {}, _extends2[name] = !annotationVisibility[name], _extends2)));
          e.stopPropagation();
        },
        icon: annotationVisibility[name] ? "tick" : "blank",
        key: name,
        text: (0, _lodash.startCase)(name)
      });
    }))
  }, /*#__PURE__*/_react["default"].createElement(_core.Button, {
    className: "veSimpleVisibilityBtn",
    style: {
      marginLeft: 10,
      marginRight: 10
    },
    minimal: true,
    intent: "primary",
    icon: "eye-open"
  })));
};
module.exports = exports.default;