import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { modifierCSS } from '../../../helpers';

const propTypes = {
  /**
   * The badge content.
   */
  children: PropTypes.node,

  /**
   * The component used for the root node.
   * Either a string to use a DOM element or a component.
   */
  tag: PropTypes.elementType,

  /**
   * The component default class.
   */
  defaultClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),

  /**
   * The className property allows you to add more classes to the component.
   */
  className: PropTypes.string,

  /**
   * The badge color.
   * NOTE: success, warning and danger colors are DEPRECATED.
   */
  color: PropTypes.oneOf(['', 'primary', 'secondary', 'tertiary', 'new', 'newtwo'/* 'success', 'warning', 'danger' */]),

  /**
   * The bagde position, so it can add space to the correct side.
   */
  position: PropTypes.oneOf(['', 'start', 'between', 'end']),

  /**
   * The badge size.
   */
  size: PropTypes.oneOf(['', 'xs', 'sm', 'lg', 'xl']),

  /**
   * Displays the badge as text only, no container around.
   */
  asText: PropTypes.bool,
};

const defaultProps = {
  children: '',
  tag: 'span',
  defaultClassName: 'sui-a-badge',
  className: '',
  color: '',
  position: '',
  size: '',
  asText: false,
};

export const Badge = React.forwardRef(({
  tag: Tag,
  defaultClassName,
  className,
  color,
  position,
  size,
  asText,
  ...props
}, ref) => {
  const classes = classnames(
    defaultClassName,
    modifierCSS(color),
    modifierCSS(position),
    modifierCSS(size),
    {
      'as--text': asText,
    },
    className,
  );

  return (
    <Tag {...props} className={classes} ref={ref} />
  );
});

Badge.propTypes = propTypes;
Badge.defaultProps = defaultProps;
