@frak-labs/core-sdk
Version:
Core SDK of the Frak wallet, low level library to interact directly with the frak ecosystem.
2 lines • 3.54 kB
JavaScript
import{n as e,r as t,t as n}from"./getCurrencyAmountKey-C-1W-BRg.js";function r(e){return Array.isArray(e)?e:e.conditions}function i(e){if(typeof e==`number`)return e;if(typeof e==`string`&&e.trim()!==``){let t=Number(e);return Number.isFinite(t)?t:void 0}}function a(e,t,n){let o=[];for(let s of r(e)){if(`logic`in s){o.push(...a(s,t,n));continue}if(s.field===t&&n.has(s.operator)){let e=i(s.value);e!=null&&o.push(e)}}return o}const o=new Set([`gt`,`gte`,`between`]),s=new Set([`gt`,`gte`]);function c(e){let t=a(e,`purchase.amount`,o);return t.length>0?Math.min(...t):void 0}function l(e){let t=a(e,`time.timestamp`,s);if(t.length!==0)return new Date(Math.min(...t)*1e3)}function u(e,t){let n=Math.round((e+t)/2/10)*10;return Math.min(t,Math.max(e,n))}function d(e){return Math.max(100,e??0)}function f(e,t){return t==null?e:e<=100&&100<=t?100:u(e,t)}function p(e,t){let n=d(t);if(n<=0)return;let r=e.percent/100*n;return e.minAmount&&(r=Math.max(r,e.minAmount.eurAmount)),e.maxAmount&&(r=Math.min(r,e.maxAmount.eurAmount)),{basket:n,reward:r}}function m(e,t,n){let r=f(t,n);if(!(r<=0))return{basket:r,reward:e/100*r}}function h(e,t){switch(e.payoutType){case`fixed`:return e.amount[t];case`percentage`:return e.maxAmount?.[t]??0;case`tiered`:return e.tiers.reduce((e,n)=>`amount`in n?Math.max(e,n.amount[t]):e,0)}}function g(e){return e.payoutType===`percentage`?e.percent:e.payoutType===`tiered`?e.tiers.reduce((e,t)=>`percent`in t?Math.max(e,t.percent):e,0):0}function _(e,t){let n=h(e,t);return n>0?n:g(e)*1e-6}function v(r,i){let a=t(i),o=n(a);switch(r.payoutType){case`fixed`:return e(Math.round(r.amount[o]),a);case`percentage`:return`${r.percent} %`;case`tiered`:{let t=h(r,o);if(t>0)return e(Math.round(t),a);let n=g(r);return n>0?`${n} %`:e(0,a)}}}function y(e,r){if(e&&!(_(e,n(t(r)))<=0))return v(e,r)}function b(e,t){return t?e.replaceAll(`{REWARD}`,t):e.replaceAll(`{REWARD}`,``)}function x(e,t){return e.expiresAt!=null&&new Date(e.expiresAt).getTime()<=t}function S(e,t){let n=l(e.conditions);return n==null||n.getTime()<=t}function C(e,t){return t===`referee`?e.referee:e.referrer}function w(e,t,n){let r=C(e,n);return r?_(r,t):0}function T(e,r={}){let i=(r.now??new Date).getTime(),a=r.audience??`referrer`,o=n(t(r.currency)),s=(r.targetInteraction?e.filter(e=>e.interactionTypeKey===r.targetInteraction):e).filter(e=>!x(e,i)),c=s.filter(e=>S(e,i));if(c.length>0)return{campaign:c.reduce((e,t)=>w(t,o,a)>w(e,o,a)?t:e),status:`live`};let u=s.map(e=>({campaign:e,startsAt:l(e.conditions)})).filter(e=>e.startsAt!=null);if(u.length===0)return;let d=u.reduce((e,t)=>t.startsAt.getTime()<e.startsAt.getTime()?t:e);return{campaign:d.campaign,status:`upcoming`,startsAt:d.startsAt}}function E(t,n={}){let r=T(t,n);if(!r)return;let i=C(r.campaign,n.audience??`referrer`);if(!i)return;let a=y(i,n.currency);if(!a)return;let o=c(r.campaign.conditions),s=o==null?void 0:e(o,n.currency),l=r.campaign.defaultLockupSeconds,u=l&&l>0?Math.round(l/86400):void 0;return{formatted:a,payoutType:i.payoutType,minPurchaseAmount:s,lockupDurationDays:u,referrerReward:r.campaign.referrer,refereeReward:r.campaign.referee,minPurchaseValue:o??void 0}}function D(e,t={}){return E(e,t)?.formatted}export{b as applyRewardPlaceholder,p as buildPercentageExample,m as buildTierExample,c as extractMinPurchaseAmount,l as extractStartDate,D as formatBestReward,v as formatEstimatedReward,y as formatRewardOrHide,h as getRewardValue,d as pickFlatBasket,f as pickTierBasket,E as selectBestReward,T as selectDisplayCampaign};
//# sourceMappingURL=rewards.js.map