"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _rangeUtils = require("@teselagen/range-utils");
var _getXStartAndWidthOfRangeWrtRow = _interopRequireDefault(require("./getXStartAndWidthOfRangeWrtRow"));
var _react = _interopRequireWildcard(require("react"));
var _calculateTickMarkPositionsForGivenRange = _interopRequireDefault(require("../utils/calculateTickMarkPositionsForGivenRange"));
var _proteinUtils = require("../utils/proteinUtils");
var _reactEasyState = require("@risingstack/react-easy-state");
var _getVisibleStartEnd = require("../utils/getVisibleStartEnd");
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 _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); }
var Axis = function Axis(props) {
  var row = props.row,
    tickSpacing = props.tickSpacing,
    bpsPerRow = props.bpsPerRow,
    charWidth = props.charWidth,
    annotationHeight = props.annotationHeight,
    marginTop = props.marginTop,
    sequenceLength = props.sequenceLength,
    _props$showAxisNumber = props.showAxisNumbers,
    showAxisNumbers = _props$showAxisNumber === void 0 ? true : _props$showAxisNumber,
    getGaps = props.getGaps,
    scrollData = props.scrollData,
    isProtein = props.isProtein,
    style = props.style,
    isLinearView = props.isLinearView;
  var noRows = row.start === 0 && row.end === 0;
  /* eslint-disable react-hooks/exhaustive-deps */
  //memoize this function because it does the heavy lifting
  var tickMarkPositions = (0, _react.useMemo)(function () {
    if (noRows) return [];
    return (0, _calculateTickMarkPositionsForGivenRange["default"])({
      tickSpacing: tickSpacing,
      range: row,
      sequenceLength: sequenceLength,
      isProtein: isProtein
    }).map(function (tickMarkPosition) {
      var gaps = getGaps ? getGaps(tickMarkPosition).gapsBefore : 0;
      var xCenter = (tickMarkPosition - (isProtein ? 1 : 0) + gaps) * charWidth + charWidth / 2;
      return {
        tickMarkPosition: tickMarkPosition,
        xCenter: xCenter
      };
    });
  }, [noRows, tickSpacing, row.start, row.end, sequenceLength, isProtein, charWidth]);
  /* eslint-enable react-hooks/exhaustive-deps*/
  if (noRows) {
    return null;
  }
  var _getXStartAndWidthOfR = (0, _getXStartAndWidthOfRangeWrtRow["default"])(_extends({
      row: row,
      range: row,
      charWidth: charWidth,
      sequenceLength: sequenceLength
    }, getGaps ? getGaps(row) : {})),
    xStart = _getXStartAndWidthOfR.xStart,
    width = _getXStartAndWidthOfR.width;
  //this function should take in a desired tickSpacing (eg 10 bps between tick mark)
  //and output an array of tickMarkPositions for the given row (eg, [0, 10, 20])
  var xEnd = xStart + width;
  var visibleStart, visibleEnd;
  if (scrollData) {
    var val = (0, _getVisibleStartEnd.getVisibleStartEnd)({
      scrollData: scrollData,
      width: sequenceLength * charWidth
    });
    //add a small buffer to either side of the visible start/end
    visibleStart = val.visibleStart - 400;
    visibleEnd = val.visibleEnd + 400;
  }
  var yStart = 0;
  // const yEnd = annotationHeight / 3;

  var tickMarkSVG = [];
  tickMarkPositions.forEach(function (_ref, i) {
    var tickMarkPosition = _ref.tickMarkPosition,
      xCenter = _ref.xCenter;
    // const xCenterPlusXStart = xCenter + xStart;

    if (scrollData && !(xCenter < visibleEnd && xCenter > visibleStart)) return;
    tickMarkSVG.push( /*#__PURE__*/_react["default"].createElement("rect", {
      className: "veAxisTick",
      key: "axisTickMarkPath " + i + " " + tickMarkPosition,
      x: xCenter,
      width: 1,
      height: 5
    }));
    if (showAxisNumbers) {
      var position = (0, _rangeUtils.normalizePositionByRangeLength)(row.start + tickMarkPosition, sequenceLength) + (isProtein ? 0 : 1);
      var positionLength = position.toString().length * 4;
      var textInner = (0, _proteinUtils.divideBy3)(position + (isProtein ? 1 : 0), isProtein);
      var x = xCenter;
      if (!isLinearView) {
        x = i === 0 //if first label in row, or last label in row, we add checks to make sure the axis number labels don't go outside of the width of the row
        ? Math.max(positionLength, xCenter) : i === tickMarkPositions.length - 1 ? Math.min(bpsPerRow * charWidth - positionLength, xCenter) : xCenter;
      }
      tickMarkSVG.push( /*#__PURE__*/_react["default"].createElement("text", {
        "data-tick-mark": textInner,
        key: "axisTickMarkText " + i + " " + tickMarkPosition,
        fill: "black",
        x: x,
        y: annotationHeight,
        style: {
          textAnchor: "middle",
          fontSize: 10,
          fontFamily: "Verdana"
        }
      }, textInner));
    }
  });
  return /*#__PURE__*/_react["default"].createElement("svg", {
    className: "veRowViewAxis veAxis",
    height: annotationHeight,
    style: _extends({
      marginTop: marginTop,
      overflow: "visible",
      display: "block"
    }, style)
  }, tickMarkSVG, /*#__PURE__*/_react["default"].createElement("path", {
    className: "veAxisLine",
    d: "M" + xStart + "," + yStart + " L" + xEnd + "," + yStart,
    stroke: "black"
  }));
};

// export default Axis
var _default = (0, _reactEasyState.view)(Axis); // export default pureNoFunc(Axis);
exports["default"] = _default;
module.exports = exports.default;