UNPKG

@frak-labs/components

Version:

Frak Wallet components, helping any person to interact with the Frak wallet.

242 lines (241 loc) 6.52 kB
//#region ../core/dist/getCurrencyAmountKey-C-1W-BRg.js const e = { eur: `fr-FR`, usd: `en-US`, gbp: `en-GB` }; function t(t) { return t && t in e ? t : `eur`; } function n(t) { return t ? e[t] ?? e.eur : e.eur; } function r$1(e, r) { let i = n(r), a = t(r); return e.toLocaleString(i, { style: `currency`, currency: a, minimumFractionDigits: 0, maximumFractionDigits: 2 }); } function i$1(e) { return e ? `${e}Amount` : `eurAmount`; } //#endregion //#region ../core/dist/rewards.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 = /* @__PURE__ */ new Set([ `gt`, `gte`, `between` ]); const s = /* @__PURE__ */ 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 /* @__PURE__ */ new Date(Math.min(...t) * 1e3); } 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 y(r, i) { let a = t(i), o = i$1(a); switch (r.payoutType) { case `fixed`: return r$1(Math.round(r.amount[o]), a); case `percentage`: return `${r.percent} %`; case `tiered`: { let t = h(r, o); if (t > 0) return r$1(Math.round(t), a); let n = g(r); return n > 0 ? `${n} %` : r$1(0, a); } } } function b(e, r) { if (e && !(_(e, i$1(t(r))) <= 0)) return y(e, r); } function x(e, t) { return t ? e.replaceAll(`{REWARD}`, t) : e.replaceAll(`{REWARD}`, ``); } const S = /* @__PURE__ */ new Set([ `eq`, `neq`, `gt`, `gte`, `lt`, `lte`, `between` ]); const w = /* @__PURE__ */ new Set([ `contains`, `starts_with`, `ends_with` ]); function D(e) { return `logic` in e && `conditions` in e; } function O(e, t) { return e[t]; } function k(e) { if (typeof e == `number`) return Number.isFinite(e) ? e : void 0; if (typeof e == `string` && e.trim() !== ``) { let t = Number(e); return Number.isFinite(t) ? t : void 0; } } function A(e, t) { let n = k(e), r = k(t); return n !== void 0 && r !== void 0 ? n - r : String(e).localeCompare(String(t)); } function j(e, t, n) { if (!Array.isArray(n)) return e === `not_in`; if (t === void 0) return; let r = n.includes(t); return e === `in` ? r : !r; } function M(e, t, n, r) { switch (e) { case `eq`: return t === n; case `neq`: return t !== n; case `gt`: return A(t, n) > 0; case `gte`: return A(t, n) >= 0; case `lt`: return A(t, n) < 0; case `lte`: return A(t, n) <= 0; case `between`: return r == null || Array.isArray(r) ? void 0 : A(t, n) >= 0 && A(t, r) <= 0; } } function N(e, t, n) { if (typeof t == `string` && typeof n == `string`) return e === `contains` ? t.includes(n) : e === `starts_with` ? t.startsWith(n) : t.endsWith(n); } function P(e, t) { let n = O(t, e.field), { operator: r, value: i, valueTo: a } = e; if (r === `exists`) return n !== void 0; if (r === `not_exists`) return n === void 0; if (r === `in` || r === `not_in`) return j(r, n, i); if (!(Array.isArray(i) || n === void 0 || i === null)) { if (S.has(r)) return M(r, n, i, a); if (w.has(r)) return N(r, n, i); } } function F(e, t) { if (D(e)) { let n = e.conditions.map((e) => F(e, t)); return e.logic === `all` ? n.every(Boolean) : e.logic === `any` ? n.some(Boolean) : e.logic !== `none` || !n.some(Boolean); } return P(e, t) ?? !0; } function I(e, t) { return e ? Array.isArray(e) ? e.every((e) => F(e, t)) : F(e, t) : !0; } function L(e, t) { return e.expiresAt != null && new Date(e.expiresAt).getTime() <= t; } function R(e, t) { let n = l(e.conditions); return n == null || n.getTime() <= t; } function z(e, t) { return t === `referee` ? e.referee : e.referrer; } function B(e, t, n) { let r = z(e, n); return r ? _(r, t) : 0; } function V(e, t) { if (!t || t.length === 0 || !e.productScope) return; let n = t.filter((t) => I(e.productScope, t)); return n.length > 0 ? n : void 0; } function H(e, t) { return !t || t.length === 0 || !e.productScope || t.some((t) => I(e.productScope, t)); } function U(e, r = {}) { let i = (r.now ?? /* @__PURE__ */ new Date()).getTime(), a = r.audience ?? `referrer`, o = i$1(t(r.currency)), s = (r.targetInteraction ? e.filter((e) => e.interactionTypeKey === r.targetInteraction) : e).filter((e) => !L(e, i)), c = s.filter((e) => R(e, i)); if (c.length > 0) { let e = c.reduce((e, t) => { let n = H(e, r.products), i = H(t, r.products); return n === i ? B(t, o, a) > B(e, o, a) ? t : e : i ? t : e; }); return { campaign: e, status: `live`, matchedProducts: V(e, r.products) }; } 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) => { let n = H(e.campaign, r.products), i = H(t.campaign, r.products); return n === i ? t.startsAt.getTime() < e.startsAt.getTime() ? t : e : i ? t : e; }); return { campaign: d.campaign, status: `upcoming`, startsAt: d.startsAt, matchedProducts: V(d.campaign, r.products) }; } function W(t, n = {}) { let r = U(t, n); if (!r) return; let i = z(r.campaign, n.audience ?? `referrer`); if (!i) return; let a = b(i, n.currency); if (!a) return; let o = c(r.campaign.conditions), s = o == null ? void 0 : r$1(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, isProductScoped: r.campaign.productScope != null, matchedProducts: r.matchedProducts }; } //#endregion export { x as i, W as n, b as r, U as t };