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