import{defineComponent as ge,shallowRef as d,computed as y,watch as V,createVNode as C}from"vue";import D from"../../_util/KeyCode.mjs";import be from"../../_util/classNames.mjs";import{functionType as f,booleanType as m,someType as I,stringType as Ne}from"../../_util/type.mjs";import pe from"./StepHandler.mjs";import Se from"./hooks/useCursor.mjs";import Ie from"./hooks/useFrame.mjs";import r,{toFixed as A}from"./utils/MiniDecimal.mjs";import{num2str as ye,validateNumber as Ve,getNumberPrecision as T}from"./utils/numberUtil.mjs";const W=(a,o)=>a||o.isEmpty()?o.toString():o.toNumber(),G=a=>{const o=r(a);return o.isInvalidate()?null:o},Ce=()=>({stringMode:m(),defaultValue:I([String,Number]),value:I([String,Number]),prefixCls:Ne(),min:I([String,Number]),max:I([String,Number]),step:I([String,Number],1),tabindex:Number,controls:m(!0),readonly:m(),disabled:m(),autofocus:m(),keyboard:m(!0),parser:f(),formatter:f(),precision:Number,decimalSeparator:String,onInput:f(),onChange:f(),onPressEnter:f(),onStep:f(),onBlur:f(),onFocus:f()}),Ae=ge({compatConfig:{MODE:3},name:"InnerInputNumber",inheritAttrs:!1,props:{...Ce(),lazy:Boolean},slots:Object,setup(a,{attrs:o,slots:J,emit:R,expose:L}){const c=d(),v=d(!1),s=d(!1),g=d(!1),l=d(r(a.value));function Q(e){a.value===void 0&&(l.value=e)}const M=(e,t)=>{if(!t)return a.precision>=0?a.precision:Math.max(T(e),T(a.step))},E=e=>{const t=String(e);if(a.parser)return a.parser(t);let n=t;return a.decimalSeparator&&(n=n.replace(a.decimalSeparator,".")),n.replace(/[^\w.-]+/g,"")},i=d(""),$=(e,t)=>{if(a.formatter)return a.formatter(e,{userTyping:t,input:String(i.value)});let n=typeof e=="number"?ye(e):e;if(!t){const u=M(n,t);if(Ve(n)&&(a.decimalSeparator||u>=0)){const S=a.decimalSeparator||".";n=A(n,S,u)}}return n},X=(()=>{const e=a.value;return l.value.isInvalidate()&&["string","number"].includes(typeof e)?Number.isNaN(e)?"":e:$(l.value.toString(),!1)})();i.value=X;function b(e,t){i.value=$(e.isInvalidate()?e.toString(!1):e.toString(!t),t)}const N=y(()=>G(a.max)),p=y(()=>G(a.min)),q=y(()=>!N.value||!l.value||l.value.isInvalidate()?!1:N.value.lessEquals(l.value)),B=y(()=>!p.value||!l.value||l.value.isInvalidate()?!1:l.value.lessEquals(p.value)),[Y,Z]=Se(c,v),F=e=>N.value&&!e.lessEquals(N.value)?N.value:p.value&&!p.value.lessEquals(e)?p.value:null,K=e=>!F(e),P=(e,t)=>{let n=e,u=K(n)||n.isEmpty();if(!n.isEmpty()&&!t&&(n=F(n)||n,u=!0),!a.readonly&&!a.disabled&&u){const S=n.toString(),x=M(S,t);return x>=0&&(n=r(A(S,".",x))),n.equals(l.value)||(Q(n),a.onChange?.(n.isEmpty()?null:W(a.stringMode,n)),a.value===void 0&&b(n,t)),n}return l.value},_=Ie(),h=e=>{if(Y(),i.value=e,!g.value){const t=E(e),n=r(t);n.isNaN()||P(n,!0)}a.onInput?.(e),_(()=>{let t=e;a.parser||(t=e.replace(/。/g,".")),t!==e&&h(t)})},ee=()=>{g.value=!0},ae=()=>{g.value=!1,h(c.value.value)},U=e=>{h(e.target.value)},k=e=>{if(e&&q.value||!e&&B.value)return;s.value=!1;let t=r(a.step);e||(t=t.negate());const n=(l.value||r(0)).add(t.toString()),u=P(n,!1);a.onStep?.(W(a.stringMode,u),{offset:a.step,type:e?"up":"down"}),c.value?.focus()},H=e=>{const t=r(E(i.value));let n=t;t.isNaN()?n=l.value:n=P(t,e),a.value!==void 0?b(l.value,!1):n.isNaN()||b(n,!1)},te=()=>{s.value=!0},ne=e=>{const{which:t}=e;s.value=!0,t===D.ENTER&&(g.value||(s.value=!1),H(!1),a.onPressEnter?.(e)),a.keyboard!==!1&&!g.value&&[D.UP,D.DOWN].includes(t)&&(k(D.UP===t),e.preventDefault())},le=()=>{s.value=!1},ue=e=>{H(!1),v.value=!1,s.value=!1,R("blur",e)};return V(()=>a.precision,()=>{l.value.isInvalidate()||b(l.value,!1)},{flush:"post"}),V(()=>a.value,()=>{const e=r(a.value);l.value=e;const t=r(E(i.value));(!e.equals(t)||!s.value||a.formatter)&&b(e,s.value)},{flush:"post"}),V(i,()=>{a.formatter&&Z()},{flush:"post"}),V(()=>a.disabled,e=>{e&&(v.value=!1)}),L({focus:()=>{c.value?.focus()},blur:()=>{c.value?.blur()}}),()=>{const{prefixCls:e="rc-input-number",min:t,max:n,step:u=1,defaultValue:S,value:x,disabled:O,readonly:z,keyboard:De,controls:re=!0,autofocus:oe,stringMode:Ee,parser:Pe,formatter:he,precision:xe,decimalSeparator:we,onChange:Re,onInput:Me,onPressEnter:$e,onStep:qe,lazy:se,class:ie,style:fe,...ce}={...o,...a},{upHandler:de,downHandler:me}=J,j=`${e}-input`,w={};return se?w.onChange=U:w.onInput=U,C("div",{class:be(e,ie,{[`${e}-focused`]:v.value,[`${e}-disabled`]:O,[`${e}-readonly`]:z,[`${e}-not-a-number`]:l.value.isNaN(),[`${e}-out-of-range`]:!l.value.isInvalidate()&&!K(l.value)}),style:fe,onKeydown:ne,onKeyup:le},[re&&C(pe,{prefixCls:e,upDisabled:q.value,downDisabled:B.value,onStep:k},{upNode:de,downNode:me}),C("div",{class:`${j}-wrap`},[C("input",{autofocus:oe,autocomplete:"off",role:"spinbutton","aria-valuemin":t,"aria-valuemax":n,"aria-valuenow":l.value.isInvalidate()?null:l.value.toString(),step:u,...ce,ref:c,class:j,value:i.value,disabled:O,readonly:z,onFocus:ve=>{v.value=!0,R("focus",ve)},...w,onBlur:ue,onCompositionstart:ee,onCompositionend:ae,onBeforeinput:te},null)])])}}});export{Ae as default,Ce as inputNumberProps};
