import{findBestPlanFromList as e}from"./calculateBestSellingPlan.js";import{convertDecimalToNumber as r}from"@gem-sdk/core";let p="__unknown__",i=e=>{let r=[];for(let p of e)if(p?.sellingPlans){let e=p.sellingPlans.filter(e=>null!=e);r.push(...e)}return r},t=e=>{if(!e)return 0;let p="number"==typeof e.price?String(e.price):e.price??void 0;return r(p)??0},n=(e,r)=>e.find(e=>e?.sellingPlans?.some(e=>e?.baseID===r||e?.id===r)),o=(r,p)=>{let t=i(r);if(0===t.length)return;let o=e({plans:t,variantPrice:p});if(o)return n(r,o.baseID||o.id||"")},l=({sellingPlanGroups:e,extractedSettings:r,hasGroup:p,hasSourceApp:i,isUnknownSourceApp:t})=>p?t?e.find(e=>e?.id===r.group):i?e.find(e=>e?.appID===r.sourceApp&&e?.id===r.group):void 0:void 0,u=e=>{let{sellingPlanGroups:r,extractedSettings:u,currentVariant:s}=e,f=i(r),d=t(s),a=!!u.group,c=u.sourceApp===p,g=!!u.sourceApp,A=l({sellingPlanGroups:r,extractedSettings:u,hasGroup:a,hasSourceApp:g,isUnknownSourceApp:c}),m=u.defaultOptions?n(r,u.defaultOptions):void 0,v=u.sourceApp&&u.sourceApp!==p?r.find(e=>e?.appID===u.sourceApp):void 0,D=!a||void 0===u.sourceApp||!!A,I=!u.defaultOptions||!!m,O=!u.sourceApp||u.sourceApp===p||!!v;if(!(D&&I&&O)){let e=o(r,d);return e||(f.length?r[0]:void 0)}if(u.defaultOptions){let e=n(r,u.defaultOptions);if(e)return e}if(u.sourceApp){let e=r.find(e=>e?.appID===u.sourceApp);if(e)return e}if(d>0){let e=o(r,d);if(e)return e}return r[0]};export{u as findMatchedGroup};
