"use strict";

exports.__esModule = true;
exports.default = void 0;

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

var _clsx2 = _interopRequireDefault(require("clsx"));

var _utils = require("../utils");

var _number = require("../utils/validate/number");

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 _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); }

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 = (0, _react.useContext)(_ConfigProviderContext.default),
      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 (0, _utils.isDef)(content) && content !== '' && (showZero || +content !== 0);
  };

  var renderContent = function renderContent() {
    if (!dot && hasContent()) {
      if ((0, _utils.isDef)(max) && (0, _number.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 = (0, _utils.addUnit)(y);
          style.right = (0, _utils.addUnit)(x);
        } else {
          style.marginTop = (0, _utils.addUnit)(y);
          style.marginLeft = (0, _utils.addUnit)(x);
        }
      }

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

      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx2.default)((_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.default.createElement(TagElement, {
      className: (0, _clsx2.default)(bem('wrapper'), props.className),
      style: props.style,
      onClick: props.onClick,
      onTouchStart: props.onTouchStart
    }, props.children, renderBadge());
  }

  return renderBadge();
};

Badge.defaultProps = {
  tag: 'div',
  showZero: true
};
var _default = Badge;
exports.default = _default;