'use strict';

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

var vue = require('vue');
var devWarning = require('../vc-util/devWarning.js');
var Compact = require('../space/Compact.js');
require('../hooks/index.js');
var DisabledContext = require('../config-provider/DisabledContext.js');
var index$1 = require('../_util/props-util/index.js');
var index$2 = require('../vc-wave/index.js');
var buttonTypes = require('./buttonTypes.js');
var LoadingIcon = require('./LoadingIcon.js');
var index = require('./style/index.js');
var buttonGroup = require('./button-group.js');
var initDefaultProps = require('../_util/props-util/initDefaultProps.js');
var useConfigInject = require('../config-provider/hooks/useConfigInject.js');

function _isSlot(s) {
  return typeof s === "function" || Object.prototype.toString.call(s) === "[object Object]" && !vue.isVNode(s);
}
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.default(buttonTypes.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 = buttonGroup.GroupSizeContext.useInject();
    const disabledContext = DisabledContext.useInjectDisabled();
    const mergedDisabled = vue.computed(() => {
      var _a;
      return (_a = props.disabled) != null ? _a : disabledContext.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
    } = Compact.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
      } = 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}-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(() => {
      devWarning.default(!(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 = index$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 buttonProps2 = {
        ...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 buttonProps2.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", vue.mergeProps(buttonProps2, {
          "href": href,
          "target": target,
          "ref": buttonNodeRef
        }), [iconNode, kids]));
      }
      let buttonNode = vue.createVNode("button", vue.mergeProps(buttonProps2, {
        "ref": buttonNodeRef,
        "type": htmlType
      }), [iconNode, kids]);
      if (!isUnBorderedButtonType(type)) {
        const _buttonNode = function() {
          return buttonNode;
        }();
        buttonNode = vue.createVNode(index$2.default, {
          "ref": "wave",
          "disabled": !!innerLoading.value
        }, _isSlot(buttonNode) ? buttonNode : {
          default: () => [_buttonNode]
        });
      }
      return wrapSSR(buttonNode);
    };
  }
});

exports.buttonProps = buttonTypes.buttonProps;
exports.default = Button;
//# sourceMappingURL=button.js.map
