import{defineComponent as P,ref as $,reactive as j,watch as G,onMounted as X,createVNode as v}from"vue";import U from"@ant-design/icons-vue/StarFilled";import m from"../_util/KeyCode.mjs";import W from"../_util/classNames.mjs";import q from"../_util/hooks/useRefs.mjs";import{findDOMNode as z}from"../_util/props-util/index.mjs";import{withInstall as J}from"../_util/type.mjs";import h from"../_util/vue-types/index.mjs";import Q from"../config-provider/hooks/useConfigInject.mjs";import{useInjectFormItemContext as Y}from"../form/FormItemContext.mjs";import Z from"../tooltip/index.mjs";import _ from"./Star.mjs";import ee from"./style/index.mjs";import{getOffsetLeft as le}from"./util.mjs";import ae from"../_util/props-util/initDefaultProps.mjs";const te=()=>({prefixCls:String,count:Number,value:Number,allowHalf:{type:Boolean,default:void 0},allowClear:{type:Boolean,default:void 0},tooltips:Array,disabled:{type:Boolean,default:void 0},character:h.any,autofocus:{type:Boolean,default:void 0},tabindex:h.oneOfType([h.number,h.string]),direction:String,id:String,onChange:Function,onHoverChange:Function,"onUpdate:value":Function,onFocus:Function,onBlur:Function,onKeydown:Function}),oe=P({compatConfig:{MODE:3},name:"ARate",inheritAttrs:!1,props:ae(te(),{value:0,count:5,allowHalf:!1,allowClear:!0,tabindex:0,direction:"ltr"}),setup(o,{slots:R,attrs:b,emit:r,expose:x}){const{prefixCls:s,direction:C}=Q("rate",o),[H,D]=ee(s),p=Y(),g=$(),[S,B]=q(),e=j({value:o.value,focused:!1,cleanedValue:null,hoverValue:void 0});G(()=>o.value,()=>{e.value=o.value});const I=l=>z(B.value.get(l)),w=(l,t)=>{const a=C.value==="rtl";let n=l+1;if(o.allowHalf){const u=I(l),f=le(u),d=u.clientWidth;(a&&t-f>d/2||!a&&t-f<d/2)&&(n-=.5)}return n},c=l=>{o.value===void 0&&(e.value=l),r("update:value",l),r("change",l),p.onFieldChange()},N=(l,t)=>{const a=w(t,l.pageX);a!==e.cleanedValue&&(e.hoverValue=a,e.cleanedValue=null),r("hoverChange",a)},y=()=>{e.hoverValue=void 0,e.cleanedValue=null,r("hoverChange",void 0)},T=(l,t)=>{const{allowClear:a}=o,n=w(t,l.pageX);let u=!1;a&&(u=n===e.value),y(),c(u?0:n),e.cleanedValue=u?n:null},M=l=>{e.focused=!0,r("focus",l)},k=l=>{e.focused=!1,r("blur",l),p.onFieldBlur()},O=l=>{const{keyCode:t}=l,{count:a,allowHalf:n}=o,u=C.value==="rtl";t===m.RIGHT&&e.value<a&&!u?(n?e.value+=.5:e.value+=1,c(e.value),l.preventDefault()):t===m.LEFT&&e.value>0&&!u||t===m.RIGHT&&e.value>0&&u?(n?e.value-=.5:e.value-=1,c(e.value),l.preventDefault()):t===m.LEFT&&e.value<a&&u&&(n?e.value+=.5:e.value+=1,c(e.value),l.preventDefault()),r("keydown",l)},F=()=>{o.disabled||g.value.focus()};x({focus:F,blur:()=>{o.disabled||g.value.blur()}}),X(()=>{const{autofocus:l,disabled:t}=o;l&&!t&&F()});const E=(l,{index:t})=>{const{tooltips:a}=o;return a?v(Z,{title:a[t]},{default:()=>[l]}):l};return()=>{const{count:l,allowHalf:t,disabled:a,tabindex:n,id:u=p.id.value}=o,{class:f,style:d}=b,V=[],K=a?`${s.value}-disabled`:"",L=o.character||R.character||(()=>v(U,null,null));for(let i=0;i<l;i++)V.push(v(_,{ref:S(i),key:i,index:i,count:l,disabled:a,prefixCls:`${s.value}-star`,allowHalf:t,value:e.hoverValue===void 0?e.value:e.hoverValue,onClick:T,onHover:N,character:L,characterRender:E,focused:e.focused},null));const A=W(s.value,K,f,{[D.value]:!0,[`${s.value}-rtl`]:C.value==="rtl"});return H(v("ul",{...b,id:u,class:A,style:d,onMouseleave:a?null:y,tabindex:a?-1:n,onFocus:a?null:M,onBlur:a?null:k,onKeydown:a?null:O,ref:g,role:"radiogroup"},[V]))}}}),ye=J(oe);export{ye as default,te as rateProps};
