"use strict";var e=require("../types.js");const t=e=>{let{value:t,defaultValue:l=0}=e;return"string"==typeof t?parseFloat(t)||l:"number"==typeof t?t:l},l=e=>{let{value:t}=e;return!isNaN(t)&&t>0},r=e=>{let{policies:t}=e,l=t.filter(e=>null!=e);if(0===l.length)return null;let r=l.find(e=>{let t=e.AfterCycle??e.afterCycle;return null==t||0===t});return r??l[0]??null},n=r=>{let{variantPrice:n,adjustmentType:u,adjustmentValue:a}=r;if(!u||!a)return n;if(u===e.AdjustmentType.FIXED_AMOUNT&&a.amount){let e=t({value:a.amount});return l({value:e})?n-e:n}if(u===e.AdjustmentType.PERCENTAGE&&void 0!==a.percentage){let e=a.percentage;return l({value:e})?n-n*e/100:n}if(u===e.AdjustmentType.PRICE&&a.amount){let e=t({value:a.amount});return l({value:e})?e:n}return n},u=e=>{if(!e)return null;let t=e.match(/(\d+(?:\.\d+)?)\s*%/);if(t&&t[1]){let e=parseFloat(t[1]);if(!isNaN(e)&&e>0&&e<=100)return e}return null},a=(t,l)=>n({variantPrice:t,adjustmentType:e.AdjustmentType.PERCENTAGE,adjustmentValue:{percentage:l}}),i=e=>{let{plan:t,variantPrice:l}=e,i=t.pricingPolicies;if(Array.isArray(i)&&i.length>0){let e=i.filter(e=>null!=e);if(e.length>0){let t=r({policies:e});if(t){let e=t.AdjustmentType??t.adjustmentType,r=t.AdjustmentValue??t.adjustmentValue;return n({variantPrice:l,adjustmentType:e,adjustmentValue:r})}}}let s=u(t.name);return null!==s?a(l,s):l};exports.findBestPlanFromList=e=>{let{plans:t,variantPrice:l}=e;if(0===t.length)return null;let r=null,n=1/0;for(let e of t){let t=i({plan:e,variantPrice:l});if(t<n)n=t,r=e;else if(t===n&&r){let t=e=>{let t=e.updatedAt;if(!t)return 0;if("string"==typeof t){let e=Date.parse(t);return isNaN(e)?parseFloat(t)||0:e}return t},l=t(e),n=t(r);l>n?r=e:l===n&&e.id>r.id&&(r=e)}}return r};
