"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _react = _interopRequireWildcard(require("react"));

var _measureTextWidth = _interopRequireDefault(require("../../../../pro/lib/_util/measureTextWidth"));

var _classnames = _interopRequireDefault(require("classnames"));

var Marks = function Marks(_ref) {
  var className = _ref.className,
      vertical = _ref.vertical,
      marks = _ref.marks,
      included = _ref.included,
      upperBound = _ref.upperBound,
      lowerBound = _ref.lowerBound,
      max = _ref.max,
      min = _ref.min,
      onClickLabel = _ref.onClickLabel;
  var marksKeys = Object.keys(marks);
  var range = max - min;
  var listRef = (0, _react.useRef)([]);
  (0, _react.useEffect)(function () {
    // 解决相邻 mark 重叠问题。
    var markCurrents = listRef.current;

    for (var i = 0; i < markCurrents.length; i++) {
      var currentParent = markCurrents[i].current;
      var currentParentChild = currentParent.firstChild;

      if (i > 0) {
        // 从第二个 mark 开始判断
        var prevParent = markCurrents[i - 1].current;
        var prevChild = prevParent.firstChild;

        if (!vertical) {
          var pevlength = prevParent.offsetLeft + prevChild.offsetLeft + prevChild.offsetWidth;

          if (currentParent.offsetLeft + currentParentChild.offsetLeft < pevlength && !prevParent.style.top) {
            currentParent.style.top = '-40px';
          }
        } else {
          var pevHeight = prevParent.offsetTop;

          if (currentParent.offsetTop + currentParent.offsetHeight > pevHeight && !prevParent.style.left) {
            currentParent.style.left = "".concat(-(currentParent.offsetWidth + 15), "px");
          }
        }
      }
    }
  }, []);
  var elements = marksKeys.map(parseFloat).sort(function (a, b) {
    return a - b;
  }).map(function (point, index) {
    var _classNames;

    var markPoint = marks[point];
    var markPointIsObject = (0, _typeof2["default"])(markPoint) === 'object' && ! /*#__PURE__*/(0, _react.isValidElement)(markPoint);
    var markLabel = markPointIsObject ? markPoint.label : markPoint;

    if (!markLabel && markLabel !== 0) {
      return null;
    }

    var isActive = !included && point === upperBound || included && point <= upperBound && point >= lowerBound;
    var markClassName = (0, _classnames["default"])((_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(className, "-text"), true), (0, _defineProperty2["default"])(_classNames, "".concat(className, "-text-active"), isActive), _classNames));
    var bottomStyle = {
      marginBottom: '-50%',
      bottom: "".concat((point - min) / range * 100, "%")
    };
    var markWidth = typeof markLabel === 'string' ? (0, _measureTextWidth["default"])(markLabel) : 0;
    var leftStyle = {
      marginLeft: "".concat(-markWidth / 2, "px"),
      left: "".concat((point - min) / range * 100, "%")
    };
    var style = vertical ? bottomStyle : leftStyle;
    var markStyle = markPointIsObject ? (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, style), markPoint.style) : style;
    var ref = /*#__PURE__*/(0, _react.createRef)();
    listRef.current.push(ref);
    return /*#__PURE__*/_react["default"].createElement("span", {
      ref: ref,
      className: markClassName,
      style: markStyle,
      key: point,
      onMouseDown: function onMouseDown(e) {
        return onClickLabel(e, point);
      },
      onTouchStart: function onTouchStart(e) {
        return onClickLabel(e, point);
      }
    }, /*#__PURE__*/_react["default"].createElement("span", null, markLabel));
  });
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: className
  }, elements);
};

var _default = Marks;
exports["default"] = _default;
//# sourceMappingURL=Marks.js.map
