import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React, { useContext } from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../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 = _objectWithoutProperties(props, ["dot", "color", "count", "className", "size", "offset"]);

  var children = restProps.children;

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

  var endSize = customSize || size;
  var badgeCls = getPrefixClassName('badge');
  var hasCount = React.useMemo(function () {
    return count || children;
  }, [count]);
  var classStyle = classNames(className, _defineProperty({}, "".concat(badgeCls, "-count"), hasCount)); // 独立渲染

  var dependencyRenderHtml = function dependencyRenderHtml() {
    var _classNames3;

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

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

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

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


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


    return /*#__PURE__*/React.cloneElement(children, _objectSpread({
      className: classNames(_defineProperty({}, "".concat(badgeCls, "-child"), !!(restProps === null || restProps === void 0 ? void 0 : restProps.children)))
    }, restProps), [children, dependencyRenderHtml()]);
  };

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

export default Badge;