import{defineComponent as O,computed as h,onBeforeMount as M,ref as v,watch as j,onMounted as D,nextTick as K,createVNode as c}from"vue";import N from"@ant-design/icons-vue/LoadingOutlined";import y from"../_util/KeyCode.mjs";import z from"../_util/omit.mjs";import{getPropsSlot as g}from"../_util/props-util/index.mjs";import{withInstall as A,tuple as P}from"../_util/type.mjs";import n from"../_util/vue-types/index.mjs";import U from"../_util/wave/index.mjs";import{useInjectDisabled as _}from"../config-provider/DisabledContext.mjs";import E from"../config-provider/hooks/useConfigInject.mjs";import{useInjectFormItemContext as H}from"../form/FormItemContext.mjs";import L from"./style/index.mjs";const R=P("small","default"),W=()=>({id:String,prefixCls:String,size:n.oneOf(R),disabled:{type:Boolean,default:void 0},checkedChildren:n.any,unCheckedChildren:n.any,tabindex:n.oneOfType([n.string,n.number]),autofocus:{type:Boolean,default:void 0},loading:{type:Boolean,default:void 0},checked:n.oneOfType([n.string,n.number,n.looseBool]),checkedValue:n.oneOfType([n.string,n.number,n.looseBool]).def(!0),unCheckedValue:n.oneOfType([n.string,n.number,n.looseBool]).def(!1),onChange:{type:Function},onClick:{type:Function},onKeydown:{type:Function},onMouseup:{type:Function},"onUpdate:checked":{type:Function},onBlur:Function,onFocus:Function}),G=O({compatConfig:{MODE:3},name:"ASwitch",__ANT_SWITCH:!0,inheritAttrs:!1,props:W(),slots:Object,setup(e,{attrs:i,slots:f,expose:b,emit:u}){const k=H(),p=_(),a=h(()=>e.disabled??p.value);M(()=>{});const s=v(e.checked!==void 0?e.checked:i.defaultChecked),m=h(()=>s.value===e.checkedValue);j(()=>e.checked,()=>{s.value=e.checked});const{prefixCls:l,direction:w,size:S}=E("switch",e),[F,x]=L(l),t=v(),C=()=>{t.value?.focus()};b({focus:C,blur:()=>{t.value?.blur()}}),D(()=>{K(()=>{e.autofocus&&!a.value&&t.value.focus()})});const r=(o,d)=>{a.value||(u("update:checked",o),u("change",o,d),k.onFieldChange())},B=o=>{u("blur",o)},T=o=>{C();const d=m.value?e.unCheckedValue:e.checkedValue;r(d,o),u("click",d,o)},V=o=>{o.keyCode===y.LEFT?r(e.unCheckedValue,o):o.keyCode===y.RIGHT&&r(e.checkedValue,o),u("keydown",o)},$=o=>{t.value?.blur(),u("mouseup",o)},I=h(()=>({[`${l.value}-small`]:S.value==="small",[`${l.value}-loading`]:e.loading,[`${l.value}-checked`]:m.value,[`${l.value}-disabled`]:a.value,[l.value]:!0,[`${l.value}-rtl`]:w.value==="rtl",[x.value]:!0}));return()=>F(c(U,null,{default:()=>[c("button",{...z(e,["prefixCls","checkedChildren","unCheckedChildren","checked","autofocus","checkedValue","unCheckedValue","id","onChange","onUpdate:checked"]),...i,id:e.id??k.id.value,onKeydown:V,onClick:T,onBlur:B,onMouseup:$,type:"button",role:"switch","aria-checked":s.value,disabled:a.value||e.loading,class:[i.class,I.value],ref:t},[c("div",{class:`${l.value}-handle`},[e.loading?c(N,{class:`${l.value}-loading-icon`},null):null]),c("span",{class:`${l.value}-inner`},[c("span",{class:`${l.value}-inner-checked`},[g(f,e,"checkedChildren")]),c("span",{class:`${l.value}-inner-unchecked`},[g(f,e,"unCheckedChildren")])])])]}))}}),ae=A(G);export{R as SwitchSizes,ae as default,W as switchProps};
