import{useMemo as e}from"react";import{useProductOfferDiscount as r,useCurrentVariant as i,useProduct as l,useProductStore as t,useQuantity as a,convertDecimalToNumber as n,shopifyPriceRounding as o,useFormatMoney as p}from"@gem-sdk/core";import{calculateSellingPlanDiscount as s}from"../helpers/product-price-breakdown/getPrice.js";import{useSubscriptionSellingType as u}from"./useSubscriptionSellingType.js";let c=()=>{let c=r(),P=i(),d=l(),g=t(e=>e.selectedSellingPlan),{isOneTime:m}=u(),{quantity:f}=a(),b=e(()=>{if(!g||m)return null;let e=P?.sellingPlanGroups??d?.sellingPlanGroups??[];for(let r of e)if(r?.sellingPlans){let e=r.sellingPlans.find(e=>e?.baseID===g,e=>e?.baseID===g);if(e)return e}return null},[P,d,g,m]),F=n(P?.price)??0,S=e(()=>!b||F<=0?0:s({variantPrice:F,pricingPolicies:b.pricingPolicies}),[b,F]),{isPrepaidSubscription:v,prepaidMultiple:y}=e(()=>{let e=b?.billingPolicy?.intervalCount,r=b?.deliveryPolicy?.intervalCount;return e&&r?{isPrepaidSubscription:e>r,prepaidMultiple:e/r}:{isPrepaidSubscription:!1,prepaidMultiple:1}},[b]),M=e(()=>b&&v?(f||1)*y:f,[v,y,f,b]),h=Math.max(0,parseFloat(o(F-S,2))),k=Math.max(0,parseFloat(o(h*(M||1),2))),x=p(k,!1),j=c*(f||1),w=parseFloat(o(j,2)),C=p(w,!1),D=o(w,2),G=parseFloat(o(k-parseFloat(D),2)),I=p(G>0?G:0,!1);return{price:F,sellingPlanDiscountPerUnit:S,priceAfterSellingPlanDiscount:h,subTotal:k,subTotalMoneyFormatted:x,offerDiscountTotal:j,discountWithQuantity:w,discountMoneyFormatted:C,totalMoneyFormatted:I}};export{c as useProductPriceBreakdown};
