import{AdjustmentType as e}from"../../components/product-subscription/types.js";let findPolicyWithoutAfterCycle=({policies:e})=>{let t=e.filter(e=>null!=e);if(0===t.length)return null;let l=t.find(e=>{let t=e?.AfterCycle??e.afterCycle;return!t});return l??t[0]??null},parseAmount=({value:e})=>{if(!e)return 0;let t=parseFloat(e);return isNaN(t)||t<=0?0:t},calculatePercentageDiscount=({variantPrice:e,percentage:t})=>isNaN(t)||t<=0?0:e*t/100,calculateFixedAmountDiscount=({amount:e})=>parseAmount({value:e}),calculatePriceDiscount=({variantPrice:e,fixedPrice:t})=>{let l=parseAmount({value:t});return l<=0?0:e-l},calculateSellingPlanDiscount=({variantPrice:t,pricingPolicies:l})=>{if(!l||0===l.length)return 0;let r=l.filter(e=>null!=e);if(0===r.length)return 0;let n=findPolicyWithoutAfterCycle({policies:r});if(!n)return 0;let u=n.AdjustmentType??n.adjustmentType,a=n.AdjustmentValue??n.adjustmentValue;return u&&a?u===e.FIXED_AMOUNT&&a.amount?calculateFixedAmountDiscount({amount:a.amount}):u===e.PERCENTAGE&&void 0!==a.percentage?calculatePercentageDiscount({variantPrice:t,percentage:a.percentage}):u===e.PRICE&&a.amount?calculatePriceDiscount({variantPrice:t,fixedPrice:a.amount}):0:0},calculateFixedAmountPrice=({variantPrice:e,amount:t})=>{let l=parseAmount({value:t});return l<=0?e:e-l},calculatePercentagePrice=({variantPrice:e,percentage:t})=>isNaN(t)||t<=0?e:e-e*t/100,calculateFixedPrice=({fixedPrice:e})=>parseAmount({value:e}),calculateRecurringPrice=({variantPrice:t,pricingPolicies:l})=>{if(!l||0===l.length)return t;let r=l.filter(e=>null!=e);if(0===r.length)return t;let n=findPolicyWithoutAfterCycle({policies:r});if(!n)return t;let u=n.AdjustmentType??n.adjustmentType,a=n.AdjustmentValue??n.adjustmentValue;return u&&a?u===e.FIXED_AMOUNT&&a.amount?calculateFixedAmountPrice({variantPrice:t,amount:a.amount}):u===e.PERCENTAGE&&void 0!==a.percentage?calculatePercentagePrice({variantPrice:t,percentage:a.percentage}):u===e.PRICE&&a.amount?calculateFixedPrice({fixedPrice:a.amount}):t:t},getIntervalText=({interval:e,intervalCount:t})=>{let l={DAY:{singular:"day",plural:"days"},WEEK:{singular:"week",plural:"weeks"},MONTH:{singular:"month",plural:"months"},YEAR:{singular:"year",plural:"years"}}[e];if(!l){let l=e.toLowerCase();return 1===t?`every ${l}`:`every ${t} ${l}`}let r=1===t?l.singular:l.plural;return 1===t?`every ${r}`:`every ${t} ${r}`},formatInterval=({deliveryPolicy:e})=>{if(!e)return"";let t=e.interval?.toUpperCase();if(!t)return"";let l=e.intervalCount??1;return getIntervalText({interval:t,intervalCount:l})};export{calculateRecurringPrice,calculateSellingPlanDiscount,findPolicyWithoutAfterCycle,formatInterval};
