"use strict";

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

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

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

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

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

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

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

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

var _defaultTo = _interopRequireDefault(require("lodash/defaultTo"));

var _ScrollNumber = _interopRequireDefault(require("./ScrollNumber"));

var _colors = require("../_util/colors");

var _reactNode = require("../_util/reactNode");

var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));

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

var _excluded = ["prefixCls", "scrollNumberPrefixCls", "children", "status", "text", "color", "count", "overflowCount", "dot", "size", "title", "offset", "style", "className", "showZero"];

var Badge = function Badge(_ref) {
  var _classNames, _classNames2, _classNames3;

  var customizePrefixCls = _ref.prefixCls,
      customizeScrollNumberPrefixCls = _ref.scrollNumberPrefixCls,
      children = _ref.children,
      status = _ref.status,
      text = _ref.text,
      color = _ref.color,
      _ref$count = _ref.count,
      count = _ref$count === void 0 ? null : _ref$count,
      _ref$overflowCount = _ref.overflowCount,
      overflowCount = _ref$overflowCount === void 0 ? 99 : _ref$overflowCount,
      _ref$dot = _ref.dot,
      dot = _ref$dot === void 0 ? false : _ref$dot,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'default' : _ref$size,
      title = _ref.title,
      offset = _ref.offset,
      style = _ref.style,
      className = _ref.className,
      _ref$showZero = _ref.showZero,
      showZero = _ref$showZero === void 0 ? false : _ref$showZero,
      restProps = (0, _objectWithoutProperties2["default"])(_ref, _excluded);

  var _useContext = (0, _react.useContext)(_ConfigContext["default"]),
      getPrefixCls = _useContext.getPrefixCls;

  var prefixCls = getPrefixCls('badge', customizePrefixCls);
  var scrollNumberPrefixCls = getPrefixCls('scroll-number', customizeScrollNumberPrefixCls); // ================================ Misc ================================

  var numberedDisplayCount = count > overflowCount ? "".concat(overflowCount, "+") : count;
  var hasStatus = status !== null && status !== undefined || color !== null && color !== undefined;
  var isZero = numberedDisplayCount === '0' || numberedDisplayCount === 0;
  var showAsDot = dot && !isZero;
  var mergedCount = showAsDot ? '' : numberedDisplayCount;
  var isHidden = (0, _react.useMemo)(function () {
    var isEmpty = mergedCount === null || mergedCount === undefined || mergedCount === '';
    return (isEmpty || isZero && !showZero) && !showAsDot;
  }, [mergedCount, isZero, showZero, showAsDot]); // Count should be cache in case hidden change it

  var countRef = (0, _react.useRef)(count);

  if (!isHidden) {
    countRef.current = count;
  }

  var livingCount = countRef.current; // We need cache count since remove motion should not change count display

  var displayCountRef = (0, _react.useRef)(mergedCount);

  if (!isHidden) {
    displayCountRef.current = mergedCount;
  }

  var displayCount = displayCountRef.current; // We will cache the dot status to avoid shaking on leaved motion

  var isDotRef = (0, _react.useRef)(showAsDot);

  if (!isHidden) {
    isDotRef.current = showAsDot;
  }

  var isDot = isDotRef.current; // =============================== Styles ===============================

  var mergedStyle = (0, _react.useMemo)(function () {
    if (!offset) {
      return (0, _objectSpread2["default"])({}, style);
    }

    var offsetStyle = {
      marginTop: offset[0],
      marginLeft: offset[1]
    };
    return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, offsetStyle), style);
  }, [offset, style]); // =============================== Render ===============================
  // >>> Title

  var titleNode = (0, _defaultTo["default"])(title, typeof livingCount === 'string' || typeof livingCount === 'number' ? livingCount : undefined); // >>> Status Text

  var statusTextNode = isHidden || !text ? null : /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefixCls, "-status-text")
  }, text); // >>> Display Component

  var displayNode = !livingCount || (0, _typeof2["default"])(livingCount) !== 'object' ? undefined : (0, _reactNode.cloneElement)(livingCount, function (oriProps) {
    return {
      style: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, mergedStyle), oriProps.style)
    };
  }); // Shared styles

  var statusCls = (0, _classnames["default"])((_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-status-dot"), hasStatus), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-status-").concat(status), !!status), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-status-").concat(color), (0, _colors.isPresetColor)(color)), _classNames));
  var scrollNumberCls = (0, _classnames["default"])((_classNames2 = {}, (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-dot"), isDot), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-count"), !isDot), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-count-sm"), size === 'small'), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-multiple-words"), !isDot && displayCount && displayCount.toString().length > 1), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-status-").concat(status), !!status), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-status-").concat(color), (0, _colors.isPresetColor)(color)), _classNames2));
  var badgeClassName = (0, _classnames["default"])(prefixCls, (_classNames3 = {}, (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-status"), hasStatus), (0, _defineProperty2["default"])(_classNames3, "".concat(prefixCls, "-not-a-wrapper"), !children), _classNames3), className);
  var statusStyle = {};

  if (color && !(0, _colors.isPresetColor)(color)) {
    statusStyle.background = color;
  }

  if (!children && hasStatus) {
    var statusTextColor = mergedStyle.color;
    return /*#__PURE__*/_react["default"].createElement("span", (0, _extends2["default"])({}, restProps, {
      className: badgeClassName,
      style: mergedStyle
    }), /*#__PURE__*/_react["default"].createElement("span", {
      className: statusCls,
      style: statusStyle
    }), /*#__PURE__*/_react["default"].createElement("span", {
      style: {
        color: statusTextColor
      },
      className: "".concat(prefixCls, "-status-text")
    }, text));
  }

  var scrollNumberStyle = (0, _objectSpread2["default"])({}, mergedStyle);

  if (color && !(0, _colors.isPresetColor)(color)) {
    scrollNumberStyle = scrollNumberStyle || {};
    scrollNumberStyle.background = color;
  }

  var scrollNumber = isHidden ? null : /*#__PURE__*/_react["default"].createElement(_ScrollNumber["default"], {
    prefixCls: scrollNumberPrefixCls,
    hidden: isHidden,
    className: scrollNumberCls,
    count: displayCount,
    title: titleNode,
    style: scrollNumberStyle,
    key: "scrollNumber"
  }, displayNode);
  return /*#__PURE__*/_react["default"].createElement("span", (0, _extends2["default"])({}, restProps, {
    className: badgeClassName
  }), children, /*#__PURE__*/_react["default"].createElement(_animate["default"], {
    component: "",
    hiddenProp: "hidden",
    transitionName: children ? "".concat(prefixCls, "-zoom") : '',
    transitionAppear: true
  }, scrollNumber, ";"), statusTextNode);
};

Badge.displayName = 'Badge';

var _default = /*#__PURE__*/(0, _react.memo)(Badge);

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