"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _vue = _interopRequireWildcard(require("vue"));

var _CloseOutlined = _interopRequireDefault(require("@ant-design/icons-vue/CloseOutlined"));

var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));

var _wave = _interopRequireDefault(require("../_util/wave"));

var _propsUtil = require("../_util/props-util");

var _BaseMixin = _interopRequireDefault(require("../_util/BaseMixin"));

var _configProvider = require("../config-provider");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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; }

var PresetColorTypes = ['pink', 'red', 'yellow', 'orange', 'cyan', 'green', 'blue', 'purple', 'geekblue', 'magenta', 'volcano', 'gold', 'lime'];
var PresetColorRegex = new RegExp("^(".concat(PresetColorTypes.join('|'), ")(-inverse)?$"));
var _default2 = {
  name: 'ATag',
  mixins: [_BaseMixin.default],
  props: {
    prefixCls: _vueTypes.default.string,
    color: _vueTypes.default.string,
    closable: _vueTypes.default.bool.def(false),
    visible: _vueTypes.default.bool,
    onClose: _vueTypes.default.func,
    'onUpdate:visible': _vueTypes.default.func
  },
  setup: function setup() {
    return {
      configProvider: (0, _vue.inject)('configProvider', _configProvider.ConfigConsumerProps)
    };
  },
  data: function data() {
    var _visible = true;
    var props = (0, _propsUtil.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 (!(0, _propsUtil.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.default, {
        "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.default, null, {
      default: function _default() {
        return [tag];
      }
    }) : tag;
  }
};
exports.default = _default2;