import{useMemo as e}from"react";import{useProductOfferDiscount as r,useCurrentVariant as i,useProduct as l,useProductStore as t,useQuantity as o,convertDecimalToNumber as n,shopifyPriceRounding as a,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 useProductPriceBreakdown=()=>{let c=r(),P=i(),d=l(),g=t(e=>e.selectedSellingPlan),{isOneTime:m}=u(),{quantity:f}=o(),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);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:k,prepaidMultiple:v}=e(()=>{let e=b?.billingPolicy?.intervalCount,r=b?.deliveryPolicy?.intervalCount;return e&&r?{isPrepaidSubscription:e>r,prepaidMultiple:e/r}:{isPrepaidSubscription:!1,prepaidMultiple:1}},[b]),y=e(()=>b&&k?(f||1)*v:f,[k,v,f,b]),M=Math.max(0,parseFloat(a(F-S,2))),h=Math.max(0,parseFloat(a(M*(y||1),2))),w=p(h,!1),x=c*(f||1),j=parseFloat(a(x,2)),B=p(j,!1),C=a(j,2),G=parseFloat(a(h-parseFloat(C),2)),D=p(G>0?G:0,!1);return{price:F,sellingPlanDiscountPerUnit:S,priceAfterSellingPlanDiscount:M,subTotal:h,subTotalMoneyFormatted:w,offerDiscountTotal:x,discountWithQuantity:j,discountMoneyFormatted:B,totalMoneyFormatted:D}};export{useProductPriceBreakdown};
