import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _extends from "@babel/runtime/helpers/extends";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

import * as React from 'react';
import classNames from 'classnames';
import omit from 'omit.js';
import CloseOutlined from '@ant-design/icons/CloseOutlined';
import CheckableTag from './CheckableTag';
import { ConfigContext } from '../config-provider';
import { PresetColorTypes, PresetStatusColorTypes } from '../_util/colors';
import Wave from '../_util/wave';
var PresetColorRegex = new RegExp("^(".concat(PresetColorTypes.join('|'), ")(-inverse)?$"));
var PresetStatusColorRegex = new RegExp("^(".concat(PresetStatusColorTypes.join('|'), ")$"));

var InternalTag = function InternalTag(props, ref) {
  var configProps = React.useContext(ConfigContext);

  var _React$useState = React.useState(true),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      visible = _React$useState2[0],
      setVisible = _React$useState2[1];

  React.useEffect(function () {
    if ('visible' in props) {
      setVisible(props.visible);
    }
  }, [props.visible]);

  var isPresetColor = function isPresetColor() {
    var color = props.color;

    if (!color) {
      return false;
    }

    return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
  };

  var getTagStyle = function getTagStyle() {
    var color = props.color,
        style = props.style;
    return _extends({
      backgroundColor: color && !isPresetColor() ? color : undefined
    }, style);
  };

  var getTagClassName = function getTagClassName(_ref) {
    var _classNames;

    var getPrefixCls = _ref.getPrefixCls,
        direction = _ref.direction;
    var customizePrefixCls = props.prefixCls,
        className = props.className,
        color = props.color;
    var presetColor = isPresetColor();
    var prefixCls = getPrefixCls('tag', customizePrefixCls);
    return classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(color), presetColor), _defineProperty(_classNames, "".concat(prefixCls, "-has-color"), color && !presetColor), _defineProperty(_classNames, "".concat(prefixCls, "-hidden"), !visible), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames), className);
  };

  var handleIconClick = function handleIconClick(e) {
    e.stopPropagation();
    var onClose = props.onClose;

    if (onClose) {
      onClose(e);
    }

    if (e.defaultPrevented) {
      return;
    }

    if (!('visible' in props)) {
      setVisible(false);
    }
  };

  var renderCloseIcon = function renderCloseIcon() {
    var closable = props.closable;
    return closable ? /*#__PURE__*/React.createElement(CloseOutlined, {
      onClick: handleIconClick
    }) : null;
  };

  var children = props.children,
      icon = props.icon,
      otherProps = __rest(props, ["children", "icon"]);

  var isNeedWave = 'onClick' in otherProps || children && children.type === 'a';
  var tagProps = omit(otherProps, ['onClose', 'color', 'visible', 'closable', 'prefixCls']);
  var iconNode = icon || null;
  var kids = iconNode ? /*#__PURE__*/React.createElement(React.Fragment, null, iconNode, /*#__PURE__*/React.createElement("span", null, children)) : children;
  return isNeedWave ? /*#__PURE__*/React.createElement(Wave, null, /*#__PURE__*/React.createElement("span", _extends({}, tagProps, {
    ref: ref,
    className: getTagClassName(configProps),
    style: getTagStyle()
  }), kids, renderCloseIcon())) : /*#__PURE__*/React.createElement("span", _extends({}, tagProps, {
    ref: ref,
    className: getTagClassName(configProps),
    style: getTagStyle()
  }), kids, renderCloseIcon());
};

var Tag = /*#__PURE__*/React.forwardRef(InternalTag);
Tag.displayName = 'Tag';
Tag.defaultProps = {
  closable: false
};
Tag.CheckableTag = CheckableTag;
export default Tag;