function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

import React, { useContext } from 'react';
import clsx from 'clsx';
import { isDef, addUnit } from '../utils';
import { isNumeric } from '../utils/validate/number';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

var Badge = function Badge(props) {
  var content = props.content,
      max = props.max,
      dot = props.dot,
      showZero = props.showZero,
      _props$tag = props.tag,
      tag = _props$tag === void 0 ? 'div' : _props$tag;

  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('badge', prefixCls),
      bem = _createNamespace[0];

  var TagElement = tag;

  var hasContent = function hasContent() {
    if (props.content) {
      return true;
    }

    return isDef(content) && content !== '' && (showZero || +content !== 0);
  };

  var renderContent = function renderContent() {
    if (!dot && hasContent()) {
      if (isDef(max) && isNumeric(content == null ? void 0 : content.toString()) && +content > max) {
        return max + "+";
      }

      return content;
    }

    return null;
  };

  var renderBadge = function renderBadge() {
    if (hasContent() || props.dot) {
      var _clsx;

      var style = {
        background: props.color
      };

      if (props.offset) {
        var _props$offset = props.offset,
            x = _props$offset[0],
            y = _props$offset[1];

        if (props.children) {
          style.top = addUnit(y);
          style.right = addUnit(x);
        } else {
          style.marginTop = addUnit(y);
          style.marginLeft = addUnit(x);
        }
      }

      if (!props.children) {
        style = _extends({}, props.style, style);
      }

      return /*#__PURE__*/React.createElement("div", {
        className: clsx((_clsx = {}, _clsx[props.className] = props.className && !props.children, _clsx), bem({
          dot: props.dot,
          fixed: !!props.children
        })),
        style: style
      }, renderContent());
    }

    return null;
  };

  if (props.children) {
    return /*#__PURE__*/React.createElement(TagElement, {
      className: clsx(bem('wrapper'), props.className),
      style: props.style,
      onClick: props.onClick,
      onTouchStart: props.onTouchStart
    }, props.children, renderBadge());
  }

  return renderBadge();
};

Badge.defaultProps = {
  tag: 'div',
  showZero: true
};
export default Badge;