import{useIsSyncProduct as t,useProduct as e}from"@gem-sdk/core";import{useState as a,useCallback as r,useEffect as u}from"react";let useHandleQuantity=(i,n,d)=>{let l=t(),o=e(),s=d?.defaultQtyValue??1,[m,p]=a(""),N=r(t=>{let e=t.detail,a=o?.id;l&&e&&a==e.productId&&i!=e.quantity&&n(e.quantity)},[l,o?.id,i,n]);u(()=>(window.addEventListener("set-product-quantity",N),()=>{window.removeEventListener("set-product-quantity",N)}),[N]);let v=t=>{let e=t.target.value,a=1>Number(e)||isNaN(Number(e))?1:Number(e);n(a),p(`${a}`)},y=t=>{isNaN(Number(t.target.value))||""===t.target.value||p(parseInt(t.target.value).toString())};return u(()=>{n(parseFloat(`${s}`)),p(`${s}`)},[n,s]),u(()=>{p(`${i}`)},[i]),{value:m,onBlurInput:v,onChangeInput:y}};export{useHandleQuantity};
