import{defineComponent as H,computed as p,shallowRef as b,watch as J,watchEffect as K,onMounted as Q,onUpdated as X,onBeforeUnmount as Y,createVNode as d,Text as Z}from"vue";import{flattenChildren as ee}from"../_util/props-util/index.mjs";import te from"../_util/wave/index.mjs";import{useInjectDisabled as oe}from"../config-provider/DisabledContext.mjs";import ne from"../config-provider/hooks/useConfigInject.mjs";import{useCompactItemContext as ae}from"../space/Compact.mjs";import le from"../vc-util/devWarning.mjs";import ue from"./LoadingIcon.mjs";import{GroupSizeContext as se}from"./button-group.mjs";import{buttonProps as ie}from"./buttonTypes.mjs";import re from"./style/index.mjs";import ce from"../_util/props-util/initDefaultProps.mjs";const M=/^[\u4E00-\u9FA5]{2}$/,E=M.test.bind(M);function g(t){return t==="text"||t==="link"}const Ne=H({compatConfig:{MODE:3},name:"AButton",inheritAttrs:!1,__ANT_BUTTON:!0,props:ce(ie(),{type:"default"}),slots:Object,setup(t,{slots:w,attrs:N,emit:I,expose:O}){const{prefixCls:s,autoInsertSpaceInButton:S,direction:k,size:P}=ne("btn",t),[z,R]=re(s),U=se.useInject(),_=oe(),h=p(()=>t.disabled??_.value),i=b(null),f=b(void 0);let C=!1;const a=b(!1),r=b(!1),B=p(()=>S.value!==!1),{compactSize:F,compactItemClassnames:L}=ae(s,k),y=p(()=>typeof t.loading=="object"&&t.loading.delay?t.loading.delay||!0:!!t.loading);J(y,e=>{clearTimeout(f.value),typeof y.value=="number"?f.value=setTimeout(()=>{a.value=e},y.value):a.value=e},{immediate:!0});const W=p(()=>{const{type:e,shape:n="default",ghost:c,block:l,danger:m}=t,o=s.value,x={large:"lg",small:"sm",middle:void 0},v=F.value||U?.size||P.value,u=v&&x[v]||"";return[L.value,{[R.value]:!0,[`${o}`]:!0,[`${o}-${n}`]:n!=="default"&&n,[`${o}-${e}`]:e,[`${o}-${u}`]:u,[`${o}-loading`]:a.value,[`${o}-background-ghost`]:c&&!g(e),[`${o}-two-chinese-chars`]:r.value&&B.value,[`${o}-block`]:l,[`${o}-dangerous`]:!!m,[`${o}-rtl`]:k.value==="rtl"}]}),j=()=>{const e=i.value;if(!e||S.value===!1)return;const n=e.textContent;C&&E(n)?r.value||(r.value=!0):r.value&&(r.value=!1)},G=e=>{if(a.value||h.value){e.preventDefault();return}I("click",e)},V=e=>{I("mousedown",e)},q=(e,n)=>{const c=n?" ":"";if(e.type===Z){let l=e.children.trim();return E(l)&&(l=l.split("").join(c)),d("span",null,[l])}return e};return K(()=>{le(!(t.ghost&&g(t.type)),"Button","`link` or `text` button can't be a `ghost` button.")}),Q(j),X(j),Y(()=>{f.value&&clearTimeout(f.value)}),O({focus:()=>{i.value?.focus()},blur:()=>{i.value?.blur()}}),()=>{const{icon:e=w.icon?.()}=t,n=ee(w.default?.());C=n.length===1&&!e&&!g(t.type);const{type:c,htmlType:l,href:m,title:o,target:x}=t,v=a.value?"loading":e,u={...N,title:o,disabled:h.value,class:[W.value,N.class,{[`${s.value}-icon-only`]:n.length===0&&!!v}],onClick:G,onMousedown:V};h.value||delete u.disabled;const D=e&&!a.value?e:d(ue,{existIcon:!!e,prefixCls:s.value,loading:!!a.value},null),A=n.map($=>q($,C&&B.value));if(m!==void 0)return z(d("a",{...u,href:m,target:x,ref:i},[D,A]));let T=d("button",{...u,ref:i,type:l},[D,A]);if(!g(c)){const $=(function(){return T})();T=d(te,{ref:"wave",disabled:!!a.value},{default:()=>[$]})}return z(T)}}});export{ie as buttonProps,Ne as default};
