import{useQuantity as t,useCurrentVariantInStock as e,useProduct as n,useIsSyncProduct as r,useInteraction as a}from"@gem-sdk/core";import{useRef as o,useEffect as u}from"react";import{useTransition as i}from"../../product-variants/common/common.js";let c=c=>{let{setting:d={},advanced:l}=c,{updatePrice:m}=d,{quantity:p,setQuantity:s,setUpdatePrice:y,increment:g,decrement:q}=t(),v=e(),I=n(),f=r(),{trigger:k}=a(),w=o(null),h=d.defaultQtyValue||1;u(()=>{s(parseFloat(`${h}`))},[s,h]),u(()=>{y(m||!1)},[m,y]);let C=t=>{let e=t.detail,n=I?.id;f&&e&&n==e.productId&&p!=e.quantity&&($(e.quantity),s(e.quantity))};u(()=>{window.removeEventListener("set-product-quantity",C),window.addEventListener("set-product-quantity",C)}),i(t=>{t!==p&&$(p||0)},[p]);let $=t=>{k({event:"gp:rollback:quantity-change",selector:`[data-id="${l?.uid}"]`,element:w.current}),k({event:"gp:quantity-change",selector:`[data-id="${l?.uid}"]`,data:t,element:w.current})};return{isInStock:v,ref:w,onClickIncrement:()=>{v&&g()},onClickDecrement:()=>{v&&q()},onChangeInput:t=>{v&&s(parseInt(t.target.value,10))},onFocusInput:t=>{v||t.target.blur()}}};export{c as useProductQuantity};
