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, { useMemo, useRef, useContext } from 'react';
import clsx from 'clsx';
import { CSSTransition } from 'react-transition-group';
import { Cross } from '@react-vant/icons';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

var Tag = function Tag(props) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

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

  var nodeRef = useRef(null);

  var onClose = function onClose(event) {
    event.stopPropagation();

    if (props.onClose) {
      props.onClose(event);
    }
  };

  var getStyle = useMemo(function () {
    if (props.plain) {
      return {
        color: props.textColor || props.color,
        borderColor: props.color
      };
    }

    return {
      color: props.textColor,
      background: props.color
    };
  }, [props.textColor, props.color, props.plain]);

  var renderTag = function renderTag() {
    var type = props.type,
        mark = props.mark,
        plain = props.plain,
        round = props.round,
        size = props.size,
        closeable = props.closeable;
    var classes = {
      mark: mark,
      plain: plain,
      round: round
    };

    if (size) {
      classes[size] = size;
    }

    var CloseIcon = closeable && /*#__PURE__*/React.createElement(Cross, {
      className: clsx(bem('close')),
      onClick: onClose
    });
    return /*#__PURE__*/React.createElement("span", {
      ref: nodeRef,
      style: _extends({}, getStyle, props.style),
      className: clsx(props.className, bem([classes, type])),
      onClick: props.onClick
    }, props.children, CloseIcon);
  };

  return /*#__PURE__*/React.createElement(CSSTransition, {
    nodeRef: nodeRef,
    classNames: "rv-fade",
    in: props.show,
    timeout: 300,
    unmountOnExit: true
  }, renderTag());
};

Tag.defaultProps = {
  show: true,
  type: 'default'
};
export default Tag;