import _extends from "@babel/runtime/helpers/extends";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
import React, { isValidElement, cloneElement, Children } from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Label from '../Label';
import Icon from '../Icon';

/**
 * `Badge` component is used for showing small numerical value or status attached to an interface element.
 * @since 0.0.58
 */
const Badge = ({
  baseClassName = `${CLS_PREFIX}badge`,
  className,
  children,
  label,
  intent,
  hidden,
  ...props
}) => {
  const isAmpty = !label;

  if (Children.toArray(children).length && !hidden) {
    if (! /*#__PURE__*/isValidElement(label)) {
      label = /*#__PURE__*/React.createElement(Label, {
        intent: "danger"
      }, label);
    }

    if ( /*#__PURE__*/isValidElement(label)) {
      const cloneProps = {
        className: classNames(`${baseClassName}__value`, {
          [`${baseClassName}__value--dot`]: isAmpty
        }, label.props.className)
      };

      if (intent && (label.type === Label || label.type === Icon)) {
        cloneProps.intent = intent;
      }

      label = /*#__PURE__*/cloneElement(label, cloneProps);
    }
  } else {
    label = null;
  }

  return /*#__PURE__*/React.createElement("div", _extends({
    className: classNames(baseClassName, className)
  }, props), children, label);
};

export default Badge;