import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { Fragment as _Fragment } from "vue";
import _extends from "@babel/runtime/helpers/esm/extends";
import { shallowRef, defineComponent, watchEffect, computed, createVNode as _createVNode } from 'vue';
import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types';
import CloseOutlined from "@ant-design/icons-vue/es/icons/CloseOutlined";
import Wave from '../_util/wave';
import { eventType } from '../_util/type';
import CheckableTag from './CheckableTag';
import CheckableTagGroup from './CheckableTagGroup';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import warning from '../_util/warning';
import useStyle from './style';
import useColor from './hooks/useColor';
export const tagProps = () => ({
  prefixCls: String,
  color: {
    type: String
  },
  closable: {
    type: Boolean,
    default: false
  },
  closeIcon: PropTypes.any,
  /** @deprecated `visible` will be removed in next major version. */
  visible: {
    type: Boolean,
    default: undefined
  },
  onClose: {
    type: Function
  },
  onClick: eventType(),
  'onUpdate:visible': Function,
  icon: PropTypes.any,
  /**
   * @deprecated Please use `variant="filled"` instead
   */
  bordered: {
    type: Boolean,
    default: true
  },
  disabled: Boolean,
  href: String,
  target: String,
  variant: String
});
const Tag = defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: 'ATag',
  inheritAttrs: false,
  props: tagProps(),
  // emits: ['update:visible', 'close'],
  slots: Object,
  setup(props, {
    slots,
    emit,
    attrs
  }) {
    const {
      prefixCls,
      direction
    } = useConfigInject('tag', props);
    const [wrapSSR, hashId] = useStyle(prefixCls);
    const visible = shallowRef(true);
    // Warning for deprecated usage
    if (process.env.NODE_ENV !== 'production') {
      warning(props.visible === undefined, 'Tag', '`visible` is deprecated, please use `<Tag v-show="visible" />` instead.');
    }
    // ====================== Colors ======================
    const colors = useColor(props);
    const isInternalColor = computed(() => colors.value.isPreset || colors.value.isStatus);
    watchEffect(() => {
      if (props.visible !== undefined) {
        visible.value = props.visible;
      }
    });
    const handleCloseClick = e => {
      if (props.disabled) {
        return;
      }
      e.stopPropagation();
      emit('update:visible', false);
      emit('close', e);
      if (e.defaultPrevented) {
        return;
      }
      if (props.visible === undefined) {
        visible.value = false;
      }
    };
    const tagClassName = computed(() => classNames(prefixCls.value, hashId.value, `${prefixCls.value}-${colors.value.mergedVariant}`, {
      [`${prefixCls.value}-${colors.value.mergedColor}`]: isInternalColor.value,
      [`${prefixCls.value}-hidden`]: !visible.value,
      [`${prefixCls.value}-rtl`]: direction.value === 'rtl',
      [`${prefixCls.value}-disabled`]: props.disabled
    }));
    const handleClick = e => {
      emit('click', e);
    };
    return () => {
      var _a, _b, _c;
      const {
        icon = (_a = slots.icon) === null || _a === void 0 ? void 0 : _a.call(slots),
        closeIcon = (_b = slots.closeIcon) === null || _b === void 0 ? void 0 : _b.call(slots),
        closable = false,
        disabled,
        href,
        target
      } = props;
      const renderCloseIcon = () => {
        if (closable) {
          return closeIcon ? _createVNode("span", {
            "class": `${prefixCls.value}-close-icon`,
            "onClick": handleCloseClick
          }, [closeIcon]) : _createVNode(CloseOutlined, {
            "class": `${prefixCls.value}-close-icon`,
            "onClick": handleCloseClick
          }, null);
        }
        return null;
      };
      let tagStyle = _extends({}, attrs.style || {});
      if (!props.disabled) {
        tagStyle = _extends(_extends({}, colors.value.customTagStyle), tagStyle);
      }
      const iconNode = icon || null;
      const children = (_c = slots.default) === null || _c === void 0 ? void 0 : _c.call(slots);
      const kids = iconNode ? _createVNode(_Fragment, null, [iconNode, _createVNode("span", null, [children])]) : children;
      const isNeedWave = props.onClick !== undefined;
      const TagWrapper = href ? 'a' : 'span';
      const tagNode = _createVNode(TagWrapper, _objectSpread(_objectSpread({}, attrs), {}, {
        "onClick": disabled ? undefined : handleClick,
        "href": disabled ? undefined : href,
        "target": target,
        "class": [tagClassName.value, attrs.class],
        "style": tagStyle
      }, href && disabled ? {
        'aria-disabled': true
      } : {}), {
        default: () => [kids, renderCloseIcon()]
      });
      return wrapSSR(isNeedWave ? _createVNode(Wave, null, {
        default: () => [tagNode]
      }) : tagNode);
    };
  }
});
Tag.CheckableTag = CheckableTag;
Tag.CheckableTagGroup = CheckableTagGroup;
Tag.install = function (app) {
  app.component(Tag.name, Tag);
  app.component(CheckableTag.name, CheckableTag);
  app.component(CheckableTagGroup.name, CheckableTagGroup);
  return app;
};
export { CheckableTag, CheckableTagGroup };
export default Tag;