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