"use strict";var types=require("../../components/product-subscription/types.js");const 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:e,pricingPolicies:t})=>{if(!t||0===t.length)return 0;let l=t.filter(e=>null!=e);if(0===l.length)return 0;let r=findPolicyWithoutAfterCycle({policies:l});if(!r)return 0;let n=r.AdjustmentType??r.adjustmentType,u=r.AdjustmentValue??r.adjustmentValue;return n&&u?n===types.AdjustmentType.FIXED_AMOUNT&&u.amount?calculateFixedAmountDiscount({amount:u.amount}):n===types.AdjustmentType.PERCENTAGE&&void 0!==u.percentage?calculatePercentageDiscount({variantPrice:e,percentage:u.percentage}):n===types.AdjustmentType.PRICE&&u.amount?calculatePriceDiscount({variantPrice:e,fixedPrice:u.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:e,pricingPolicies:t})=>{if(!t||0===t.length)return e;let l=t.filter(e=>null!=e);if(0===l.length)return e;let r=findPolicyWithoutAfterCycle({policies:l});if(!r)return e;let n=r.AdjustmentType??r.adjustmentType,u=r.AdjustmentValue??r.adjustmentValue;return n&&u?n===types.AdjustmentType.FIXED_AMOUNT&&u.amount?calculateFixedAmountPrice({variantPrice:e,amount:u.amount}):n===types.AdjustmentType.PERCENTAGE&&void 0!==u.percentage?calculatePercentagePrice({variantPrice:e,percentage:u.percentage}):n===types.AdjustmentType.PRICE&&u.amount?calculateFixedPrice({fixedPrice:u.amount}):e:e},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})};exports.calculateRecurringPrice=calculateRecurringPrice,exports.calculateSellingPlanDiscount=calculateSellingPlanDiscount,exports.findPolicyWithoutAfterCycle=findPolicyWithoutAfterCycle,exports.formatInterval=formatInterval;
