'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../wave/index.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
require('../../config-provider/index.cjs');
require('../../space/index.cjs');
var index = require('../style/index.cjs');
var LoadingIcon = require('./LoadingIcon.cjs');
var props = require('./props.cjs');
var context = require('./context.cjs');
var initDefaultProps = require('../../../utils/vue/initDefaultProps.cjs');
var disabledContext = require('../../config-provider/src/context/disabledContext.cjs');
var context$1 = require('../../space/src/context.cjs');
var log = require('../../../utils/log.cjs');
var props$1 = require('../../../utils/vue/props.cjs');
var Wave = require('../../wave/src/Wave.cjs');

const rxTwoCNChar = /^[\u4E00-\u9FA5]{2}$/;
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
function isUnBorderedButtonType(type) {
  return type === "text" || type === "link";
}
var Button = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AButton",
  inheritAttrs: false,
  __ANT_BUTTON: true,
  props: initDefaultProps.initDefaultProps(props.buttonProps(), {
    type: "default"
  }),
  slots: Object,
  // emits: ['click', 'mousedown'],
  setup(props, {
    slots,
    attrs,
    emit,
    expose
  }) {
    const {
      prefixCls,
      autoInsertSpaceInButton,
      direction,
      size
    } = useConfigInject.default("btn", props);
    const [wrapSSR, hashId] = index.default(prefixCls);
    const groupSizeContext = context.GroupSizeContext.useInject();
    const disabledContext$1 = disabledContext.useInjectDisabled();
    const mergedDisabled = vue.computed(() => {
      var _a;
      return (_a = props.disabled) != null ? _a : disabledContext$1.value;
    });
    const buttonNodeRef = vue.shallowRef(null);
    const delayTimeoutRef = vue.shallowRef(void 0);
    let isNeedInserted = false;
    const innerLoading = vue.shallowRef(false);
    const hasTwoCNChar = vue.shallowRef(false);
    const autoInsertSpace = vue.computed(() => autoInsertSpaceInButton.value !== false);
    const {
      compactSize,
      compactItemClassnames
    } = context$1.useCompactItemContext(prefixCls, direction);
    const loadingOrDelay = vue.computed(() => typeof props.loading === "object" && props.loading.delay ? props.loading.delay || true : !!props.loading);
    vue.watch(loadingOrDelay, (val) => {
      clearTimeout(delayTimeoutRef.value);
      if (typeof loadingOrDelay.value === "number") {
        delayTimeoutRef.value = setTimeout(() => {
          innerLoading.value = val;
        }, loadingOrDelay.value);
      } else {
        innerLoading.value = val;
      }
    }, {
      immediate: true
    });
    const classes = vue.computed(() => {
      const {
        type,
        shape = "default",
        ghost,
        block,
        danger,
        success
      } = props;
      const pre = prefixCls.value;
      const sizeClassNameMap = {
        large: "lg",
        small: "sm",
        middle: void 0
      };
      const sizeFullname = compactSize.value || (groupSizeContext == null ? void 0 : groupSizeContext.size) || size.value;
      const sizeCls = sizeFullname ? sizeClassNameMap[sizeFullname] || "" : "";
      return [compactItemClassnames.value, {
        [hashId.value]: true,
        [`${pre}`]: true,
        [`${pre}-${shape}`]: shape !== "default" && shape,
        [`${pre}-${type}`]: type,
        [`${pre}-${sizeCls}`]: sizeCls,
        [`${pre}-loading`]: innerLoading.value,
        [`${pre}-background-ghost`]: ghost && !isUnBorderedButtonType(type),
        [`${pre}-two-chinese-chars`]: hasTwoCNChar.value && autoInsertSpace.value,
        [`${pre}-block`]: block,
        [`${pre}-dangerous`]: !!danger,
        [`${pre}-warning`]: !!props.warning,
        [`${pre}-success`]: !!success,
        [`${pre}-rtl`]: direction.value === "rtl"
      }];
    });
    const fixTwoCNChar = () => {
      const node = buttonNodeRef.value;
      if (!node || autoInsertSpaceInButton.value === false)
        return;
      const buttonText = node.textContent;
      if (isNeedInserted && isTwoCNChar(buttonText)) {
        if (!hasTwoCNChar.value)
          hasTwoCNChar.value = true;
      } else if (hasTwoCNChar.value) {
        hasTwoCNChar.value = false;
      }
    };
    const handleClick = (event) => {
      if (innerLoading.value || mergedDisabled.value) {
        event.preventDefault();
        return;
      }
      emit("click", event);
    };
    const handleMousedown = (event) => {
      emit("mousedown", event);
    };
    const insertSpace = (child, needInserted) => {
      const SPACE = needInserted ? " " : "";
      if (child.type === vue.Text) {
        let text = child.children.trim();
        if (isTwoCNChar(text))
          text = text.split("").join(SPACE);
        return vue.createVNode("span", null, [text]);
      }
      return child;
    };
    vue.watchEffect(() => {
      log.devWarning(!(props.ghost && isUnBorderedButtonType(props.type)), "Button", "`link` or `text` button can't be a `ghost` button.");
    });
    vue.onMounted(fixTwoCNChar);
    vue.onUpdated(fixTwoCNChar);
    vue.onBeforeUnmount(() => {
      delayTimeoutRef.value && clearTimeout(delayTimeoutRef.value);
    });
    const focus = () => {
      var _a;
      (_a = buttonNodeRef.value) == null ? void 0 : _a.focus();
    };
    const blur = () => {
      var _a;
      (_a = buttonNodeRef.value) == null ? void 0 : _a.blur();
    };
    expose({
      focus,
      blur
    });
    return () => {
      var _a, _b;
      const {
        icon = (_a = slots.icon) == null ? void 0 : _a.call(slots)
      } = props;
      const children = props$1.flattenChildren((_b = slots.default) == null ? void 0 : _b.call(slots));
      isNeedInserted = children.length === 1 && !icon && !isUnBorderedButtonType(props.type);
      const {
        type,
        htmlType,
        href,
        title,
        target
      } = props;
      const iconType = innerLoading.value ? "loading" : icon;
      const getButtonProps = {
        ...attrs,
        title,
        disabled: mergedDisabled.value,
        class: [classes.value, attrs.class, {
          [`${prefixCls.value}-icon-only`]: children.length === 0 && !!iconType
        }],
        onClick: handleClick,
        onMousedown: handleMousedown
      };
      if (!mergedDisabled.value)
        delete getButtonProps.disabled;
      const iconNode = icon && !innerLoading.value ? icon : vue.createVNode(LoadingIcon.default, {
        "existIcon": !!icon,
        "prefixCls": prefixCls.value,
        "loading": !!innerLoading.value
      }, null);
      const kids = children.map((child) => insertSpace(child, isNeedInserted && autoInsertSpace.value));
      if (href !== void 0) {
        return wrapSSR(vue.createVNode("a", {
          ...getButtonProps,
          "href": href,
          "target": target,
          "ref": buttonNodeRef
        }, [iconNode, kids]));
      }
      let buttonNode = vue.createVNode("button", {
        ...getButtonProps,
        "ref": buttonNodeRef,
        "type": htmlType
      }, [iconNode, kids]);
      if (!isUnBorderedButtonType(type)) {
        const _buttonNode = function() {
          return buttonNode;
        }();
        buttonNode = vue.createVNode(Wave.default, {
          "ref": "wave",
          "disabled": !!innerLoading.value
        }, {
          default: () => [_buttonNode]
        });
      }
      return wrapSSR(buttonNode);
    };
  }
});

exports.default = Button;
//# sourceMappingURL=Button.cjs.map
