import{useRef as e,useMemo as t}from"react";import{useTransition as r}from"../../product-variants/common/common.js";import{useEditorMode as c,useQuantity as i,useCurrentVariant as a,useProductBundleDiscount as o,convertDecimalToNumber as n,useInteraction as u,useProduct as p,useShopStore as d,useFormatMoney as l}from"@gem-sdk/core";let m=m=>{let{setting:s,builderProps:g}=m,b=e(null),h=c(),{quantity:y,hasUpdatePrice:P}=i(),v=a(),{useProductCompareAtPrice:f}=o(),w=m["bundle-item"],V=n(v?.price)??0,k=n(v?.salePrice)??0,q=s?.priceType==="compare",{trigger:C}=u(),N=p(),T=N?.baseID?.replace("gid://shopify/Product/",""),x=d(e=>e.showPriceCurrency),D=t(()=>w?.quantity?w.quantity:P&&y||1,[y,P,w?.quantity]),E=t(()=>{let e=Number(w?.discountValue);return w?.discountType==="percentage"?V*D*e/100:e},[w?.discountType,w?.discountValue,V,D]),I=t(()=>w?.discountValue?f?(k||V)*D:V*D:k*D,[w?.discountValue,V,D,k,f]),$=t(()=>{let e=V*D;return q?I:E?Math.max(0,e-E):e},[V,D,E,I,q]),j=t(()=>!!q&&(!(k>V)||!(k>=0)),[q,k,V]),G=(e,t)=>{C({event:"price"===t?"gp:rollback:price-change":"gp:rollback:compare-price-change",selector:`[data-id="${g?.uid}"]`,element:b.current}),C({event:"price"===t?"gp:price-change":"gp:compare-price-change",data:e,selector:`[data-id="${g?.uid}"]`,element:b.current})};r(e=>{e!==V&&G(V,"price")},[V]),r(e=>{e!==k&&G(k,"compare")},[k]);let L=l($??0,x||!1),M=q&&!$;return{elementRef:b,moneyFormatted:L,currentProductId:T,isNoComparePrice:M,isNoComparePriceILP:"edit"!==h&&M,emitNoDataSignal:()=>{if("edit"!==h)return;let e=new CustomEvent("editor:product-source-no-data-warning",{bubbles:!0,detail:{uid:g?.uid,value:!$&&q,tag:"ProductPrice"}});window.dispatchEvent(e)},shouldHiddenComparePrice:j}};export{m as useGpPrice};
