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