"use strict";

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

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

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 _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

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

var _context = require("../config-provider/context");

var Badge = function Badge(props) {
  var _props$dot = props.dot,
      dot = _props$dot === void 0 ? false : _props$dot,
      color = props.color,
      _props$count = props.count,
      count = _props$count === void 0 ? 0 : _props$count,
      className = props.className,
      customSize = props.size,
      _props$offset = props.offset,
      offset = _props$offset === void 0 ? [0, 0] : _props$offset,
      restProps = (0, _objectWithoutProperties2.default)(props, ["dot", "color", "count", "className", "size", "offset"]);
  var children = restProps.children;

  var _useContext = (0, _react.useContext)(_context.ConfigContext),
      getPrefixClassName = _useContext.getPrefixClassName,
      size = _useContext.size;

  var endSize = customSize || size;
  var badgeCls = getPrefixClassName('badge');

  var hasCount = _react.default.useMemo(function () {
    return count || children;
  }, [count]);

  var classStyle = (0, _classnames.default)(className, (0, _defineProperty2.default)({}, "".concat(badgeCls, "-count"), hasCount)); // 独立渲染

  var dependencyRenderHtml = function dependencyRenderHtml() {
    var _classNames3;

    // 渲染 dot
    var styl = {
      background: "".concat(color),
      top: 0,
      right: 0
    };

    if (offset) {
      styl = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, styl), {}, {
        top: offset[0],
        right: offset[1],
        border: restProps.border
      });
    }

    if (dot) {
      return /*#__PURE__*/_react.default.createElement('span', {
        className: (0, _classnames.default)("".concat(badgeCls, "-noText"), (0, _defineProperty2.default)({}, "".concat(badgeCls, "-hasc"), !!(restProps === null || restProps === void 0 ? void 0 : restProps.children))),
        style: {
          background: "".concat(color)
        }
      }, '');
    }

    return /*#__PURE__*/_react.default.createElement("span", {
      className: (0, _classnames.default)(classStyle, (_classNames3 = {}, (0, _defineProperty2.default)(_classNames3, "".concat(badgeCls, "-hasc"), !!(restProps === null || restProps === void 0 ? void 0 : restProps.children)), (0, _defineProperty2.default)(_classNames3, "".concat(badgeCls, "-").concat(endSize), !!endSize), _classNames3)),
      style: styl
    }, count);
  }; // 渲染childNode


  var renderChildNode = function renderChildNode() {
    if (!children) {
      return dependencyRenderHtml();
    } // 只有一个 children


    return /*#__PURE__*/_react.default.cloneElement(children, (0, _objectSpread2.default)({
      className: (0, _classnames.default)((0, _defineProperty2.default)({}, "".concat(badgeCls, "-child"), !!(restProps === null || restProps === void 0 ? void 0 : restProps.children)))
    }, restProps), [children, dependencyRenderHtml()]);
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderChildNode());
};

var _default = Badge;
exports.default = _default;