"use strict";

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _animate = _interopRequireDefault(require("../animate"));

var _util = require("../util");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * badge sup component
 */
// util::getDigitArray
var getDigitArray = function getDigitArray(num) {
  return num.toString().split('').reverse().map(function (i) {
    return parseInt(i, 10);
  });
};

var Sup =
/*#__PURE__*/
function (_Component) {
  _inherits(Sup, _Component);

  _createClass(Sup, null, [{
    key: "renderDigit",
    // 单排可滚动的数字列表
    value: function renderDigit(prefix, digit, key) {
      var children = [];

      for (var i = 0; i < 30; i++) {
        children.push(_react["default"].createElement("span", {
          key: i
        }, i % 10));
      }

      return _react["default"].createElement("span", {
        className: "".concat(prefix, "badge-scroll-number-only"),
        key: key
      }, children);
    } // 可滚动数字组

  }, {
    key: "renderNumber",
    value: function renderNumber(prefix, count) {
      return getDigitArray(count).map(function (digit, i) {
        return Sup.renderDigit(prefix, digit, i);
      }).reverse();
    }
  }]);

  function Sup(props) {
    var _this;

    _classCallCheck(this, Sup);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Sup).call(this, props)); // 记录最后一次显示的数字

    _this.lastCount = 0;
    return _this;
  }

  _createClass(Sup, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.computeStyle(true);
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if ('count' in nextProps) {
        if (nextProps.count !== this.props.count) {
          this.lastCount = this.props.count;
        }
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var _this2 = this;

      if (prevProps.count !== this.props.count) {
        this.computeStyle(false); // NOTE why called `computeStyle` again after 300ms ?

        setTimeout(function () {
          _this2.computeStyle(true, true);
        }, 300);
      }
    }
  }, {
    key: "computeStyle",
    value: function computeStyle(removeTransition, revert) {
      var _this3 = this;

      var _this$props = this.props,
          prefix = _this$props.prefix,
          count = _this$props.count,
          overflowCount = _this$props.overflowCount;

      if (count < 0) {
        return;
      }

      var supNode = this.refs.sup;

      if (supNode && _util.dom.hasClass(supNode, "".concat(prefix, "badge-count"))) {
        var scrollNums = supNode.querySelectorAll(".".concat(prefix, "badge-scroll-number-only"));

        if (scrollNums.length) {
          var _window$getComputedSt = window.getComputedStyle(supNode),
              height = _window$getComputedSt.height;

          scrollNums = [].slice.call(scrollNums, 0).reverse();
          getDigitArray(count).forEach(function (digit, i) {
            var position = _this3.getPositionByDigit(digit, i, revert); // transform用百分比，防止缩放时的取整
            // 不支持的保留精度，防止出现小数情况


            var transformTo = -position * parseFloat(height);
            removeTransition = removeTransition || typeof getDigitArray(_this3.lastCount)[i] === 'undefined' || _this3.lastCount > overflowCount || _this3.lastCount <= 0;
            var scrollStyle = _util.support.animation ? {
              transition: _this3.props.noTransition || removeTransition ? 'none' : 'transform .3s cubic-bezier(.645, .045, .355, 1), -webkit-transform .3s cubic-bezier(.645, .045, .355, 1)',
              WebkitTransform: "translateY(".concat(-position * 100, "%)"),
              transform: "translateY(".concat(-position * 100, "%)"),
              height: height,
              lineHeight: height
            } : {
              top: "".concat(transformTo, "px"),
              height: height,
              lineHeight: height
            };
            Object.keys(scrollStyle).forEach(function (key) {
              scrollNums[i].style[key] = scrollStyle[key];
            });
          });
        }
      }
    }
  }, {
    key: "getPositionByDigit",
    value: function getPositionByDigit(digit, i, revert) {
      if (revert) {
        return 10 + digit;
      }

      var lastDigit = getDigitArray(this.lastCount)[i] || 0;

      if (this.props.count > this.lastCount) {
        return (digit >= lastDigit ? 10 : 20) + digit;
      }

      if (digit <= lastDigit) {
        return 10 + digit;
      }

      return digit;
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props2 = this.props,
          prefix = _this$props2.prefix,
          count = _this$props2.count,
          showZero = _this$props2.showZero,
          overflowCount = _this$props2.overflowCount,
          dot = _this$props2.dot,
          noTransition = _this$props2.noTransition,
          style = _this$props2.style,
          content = _this$props2.content;
      var supClasses = (0, _classnames["default"])("".concat(prefix, "badge-scroll-number"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "badge-count"), !!count || count === 0 && showZero), _defineProperty(_classNames, "".concat(prefix, "badge-dot"), dot), _defineProperty(_classNames, "".concat(prefix, "badge-custom"), !!content), _classNames));
      var children = null;
      var show = dot || count > 0 || count === 0 && showZero || content;

      if (count > 0 || count === 0 && showZero) {
        var realCount = overflowCount > 0 && count > overflowCount ? "".concat(overflowCount, "+") : count;
        children = isNaN(realCount) ? realCount : Sup.renderNumber(prefix, count);
      } else if (content) {
        children = content;
      } // const animation = {
      //   appear: 'zoomIn',
      //   enter: 'zoomIn',
      //   leave: 'zoomOut',
      // };
      // const wrapper = support.animation ? <Animate animation={animation} /> : <span />;


      var wrapper = _react["default"].createElement("span", null);

      var element = show ? _react["default"].createElement("sup", {
        ref: "sup",
        className: supClasses,
        style: style
      }, children) : null;
      return _react["default"].cloneElement(wrapper, {}, element);
    }
  }]);

  return Sup;
}(_react.Component);

exports["default"] = Sup;

_defineProperty(Sup, "propTypes", {
  prefix: _propTypes["default"].string,
  count: _propTypes["default"].number,
  showZero: _propTypes["default"].bool,
  overflowCount: _propTypes["default"].number,
  content: _propTypes["default"].node,
  dot: _propTypes["default"].bool,
  noTransition: _propTypes["default"].bool,
  // 是否显示滚动动画，tabs 中的 badge 默认不显示
  style: _propTypes["default"].object
});

_defineProperty(Sup, "defaultProps", {
  prefix: 'next-',
  count: 0,
  showZero: false,
  overflowCount: 99,
  dot: false,
  noTransition: false
});