import * as _vue from "vue";

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { inject } from 'vue';
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
import PropTypes from '../_util/vue-types';
import Wave from '../_util/wave';
import { hasProp, getOptionProps } from '../_util/props-util';
import BaseMixin from '../_util/BaseMixin';
import { ConfigConsumerProps } from '../config-provider';
var PresetColorTypes = ['pink', 'red', 'yellow', 'orange', 'cyan', 'green', 'blue', 'purple', 'geekblue', 'magenta', 'volcano', 'gold', 'lime'];
var PresetColorRegex = new RegExp("^(".concat(PresetColorTypes.join('|'), ")(-inverse)?$"));
export default {
  name: 'ATag',
  mixins: [BaseMixin],
  props: {
    prefixCls: PropTypes.string,
    color: PropTypes.string,
    closable: PropTypes.bool.def(false),
    visible: PropTypes.bool,
    onClose: PropTypes.func,
    'onUpdate:visible': PropTypes.func
  },
  setup: function setup() {
    return {
      configProvider: inject('configProvider', ConfigConsumerProps)
    };
  },
  data: function data() {
    var _visible = true;
    var props = getOptionProps(this);

    if ('visible' in props) {
      _visible = this.visible;
    }

    return {
      _visible: _visible
    };
  },
  watch: {
    visible: function visible(val) {
      this.setState({
        _visible: val
      });
    }
  },
  methods: {
    setVisible: function setVisible(visible, e) {
      this.$emit('update:visible', false);
      this.$emit('close', e);

      if (e.defaultPrevented) {
        return;
      }

      if (!hasProp(this, 'visible')) {
        this.setState({
          _visible: visible
        });
      }
    },
    handleIconClick: function handleIconClick(e) {
      e.stopPropagation();
      this.setVisible(false, e);
    },
    isPresetColor: function isPresetColor() {
      var color = this.$props.color;

      if (!color) {
        return false;
      }

      return PresetColorRegex.test(color);
    },
    getTagStyle: function getTagStyle() {
      var color = this.$props.color;
      var isPresetColor = this.isPresetColor();
      return {
        backgroundColor: color && !isPresetColor ? color : undefined
      };
    },
    getTagClassName: function getTagClassName(prefixCls) {
      var _ref;

      var color = this.$props.color;
      var isPresetColor = this.isPresetColor();
      return _ref = {}, _defineProperty(_ref, prefixCls, true), _defineProperty(_ref, "".concat(prefixCls, "-").concat(color), isPresetColor), _defineProperty(_ref, "".concat(prefixCls, "-has-color"), color && !isPresetColor), _ref;
    },
    renderCloseIcon: function renderCloseIcon() {
      var closable = this.$props.closable;
      return closable ? _vue.createVNode(CloseOutlined, {
        "onClick": this.handleIconClick
      }, null) : null;
    }
  },
  render: function render() {
    var customizePrefixCls = this.$props.prefixCls;
    var isNeedWave = ('onClick' in this.$attrs);
    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('tag', customizePrefixCls);
    var visible = this.$data._visible;

    var tag = _vue.withDirectives(_vue.createVNode("span", {
      "class": this.getTagClassName(prefixCls),
      "style": this.getTagStyle()
    }, [this.$slots.default && this.$slots.default(), this.renderCloseIcon()]), [[_vue.vShow, visible]]);

    return isNeedWave ? _vue.createVNode(Wave, null, {
      default: function _default() {
        return [tag];
      }
    }) : tag;
  }
};