"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _draggableClassnames = _interopRequireDefault(require("../../constants/draggableClassnames"));
var _react = _interopRequireDefault(require("react"));
var _Caret = _interopRequireDefault(require("../Caret"));
var _pureNoFunc = _interopRequireDefault(require("../../utils/pureNoFunc"));
require("./style.css");
var _getXStartAndWidthOfRangeWrtRow = _interopRequireDefault(require("../getXStartAndWidthOfRangeWrtRow"));
var _rangeUtils = require("@teselagen/range-utils");
var _editorUtils = require("../../utils/editorUtils");
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 SelectionLayer(props) {
  var charWidth = props.charWidth,
    isDraggable = props.isDraggable,
    row = props.row,
    sequenceLength = props.sequenceLength,
    regions = props.regions,
    _props$leftMargin = props.leftMargin,
    leftMargin = _props$leftMargin === void 0 ? 0 : _props$leftMargin,
    isProtein = props.isProtein,
    getGaps = props.getGaps,
    topLevelHideTitle = props.hideTitle,
    topLevelCustomTitle = props.customTitle,
    topLevelColor = props.color,
    _props$hideCarets = props.hideCarets,
    topLevelHideCarets = _props$hideCarets === void 0 ? false : _props$hideCarets,
    selectionLayerRightClicked = props.selectionLayerRightClicked,
    _props$className = props.className,
    globalClassname = _props$className === void 0 ? "" : _props$className,
    onClick = props.onClick;
  var hasSelection = false;
  var toReturn = /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, regions.map(function (selectionLayer, topIndex) {
    var _onClick = onClick ? function (event) {
      onClick({
        event: event,
        annotation: selectionLayer
      });
    } : undefined;
    var _selectionLayer$class = selectionLayer.className,
      className = _selectionLayer$class === void 0 ? "" : _selectionLayer$class,
      _selectionLayer$style = selectionLayer.style,
      style = _selectionLayer$style === void 0 ? {} : _selectionLayer$style,
      start = selectionLayer.start,
      end = selectionLayer.end,
      color = selectionLayer.color,
      hideTitle = selectionLayer.hideTitle,
      customTitle = selectionLayer.customTitle,
      _selectionLayer$hideC = selectionLayer.hideCarets,
      hideCarets = _selectionLayer$hideC === void 0 ? false : _selectionLayer$hideC,
      ignoreGaps = selectionLayer.ignoreGaps,
      height = selectionLayer.height;
    var selectionMessage = hideTitle || topLevelHideTitle ? "" : (0, _editorUtils.getSelectionMessage)({
      selectionLayer: selectionLayer,
      customTitle: customTitle || topLevelCustomTitle,
      sequenceLength: sequenceLength,
      isProtein: isProtein
    });
    var onSelectionContextMenu = function onSelectionContextMenu(event) {
      selectionLayerRightClicked && selectionLayerRightClicked({
        event: event,
        annotation: selectionLayer
      });
    };
    var classNameToPass = className + " " + globalClassname;
    if (start > -1) {
      var overlaps = (0, _rangeUtils.getOverlapsOfPotentiallyCircularRanges)(selectionLayer, row, sequenceLength);
      //DRAW SELECTION LAYER
      if (overlaps.length) hasSelection = true;
      return overlaps.map(function (overlap, index) {
        var key = topIndex + "-" + index;
        var isTrueStart = false;
        var isTrueEnd = false;
        if (overlap.start === selectionLayer.start) {
          isTrueStart = true;
        }
        if (overlap.end === selectionLayer.end) {
          isTrueEnd = true;
        }
        var _getXStartAndWidthOfR = (0, _getXStartAndWidthOfRangeWrtRow["default"])(_extends({
            range: overlap,
            row: row,
            charWidth: charWidth,
            sequenceLength: sequenceLength
          }, ignoreGaps ? {} : getGaps(overlap))),
          xStart = _getXStartAndWidthOfR.xStart,
          width = _getXStartAndWidthOfR.width;
        var caretSvgs = [];
        if (!(hideCarets || topLevelHideCarets)) {
          //DRAW CARETS
          caretSvgs = [overlap.start === start && /*#__PURE__*/_react["default"].createElement(_Caret["default"], {
            leftMargin: leftMargin,
            onClick: _onClick || _editorUtils.preventDefaultStopPropagation,
            onRightClick: onSelectionContextMenu,
            charWidth: charWidth,
            row: row,
            style: selectionLayer.style,
            getGaps: getGaps,
            isDraggable: isDraggable,
            ignoreGaps: ignoreGaps,
            key: key + "caret1",
            sequenceLength: sequenceLength,
            className: classNameToPass + " selectionLayerCaret " + (isDraggable ? _draggableClassnames["default"].selectionStart : ""),
            caretPosition: overlap.start
          }), overlap.end === end && /*#__PURE__*/_react["default"].createElement(_Caret["default"], {
            leftMargin: leftMargin,
            onClick: _onClick || _editorUtils.preventDefaultStopPropagation,
            onRightClick: onSelectionContextMenu,
            charWidth: charWidth,
            isDraggable: isDraggable,
            row: row,
            getGaps: getGaps,
            style: selectionLayer.style,
            ignoreGaps: ignoreGaps,
            key: key + "caret2",
            sequenceLength: sequenceLength,
            className: classNameToPass + " selectionLayerCaret " + (isDraggable ? _draggableClassnames["default"].selectionEnd : ""),
            caretPosition: overlap.end + 1
          })];
        }
        return [/*#__PURE__*/_react["default"].createElement("div", {
          onClick: _onClick,
          title: selectionMessage,
          onContextMenu: onSelectionContextMenu,
          key: key,
          className: classNameToPass + " veSelectionLayer veRowViewSelectionLayer notCaret " + (isTrueStart ? " isTrueStart " : "") + (isTrueEnd ? " isTrueEnd " : ""),
          style: _extends({
            width: width,
            left: leftMargin + xStart
          }, style, {
            background: color || topLevelColor,
            height: height
          })
        })].concat(caretSvgs);
      });
    } else {
      return null;
    }
  }));
  return hasSelection ? toReturn : null;
}
var _default = (0, _pureNoFunc["default"])(SelectionLayer);
exports["default"] = _default;
module.exports = exports.default;