UNPKG

@beamimpact/web-sdk

Version:

The Beam SDK enables brands to connect with their customers over shared values, not transactional discounts, to build stronger loyalty. Our integration achieves this by allowing customers to (a) choose a nonprofit where the brand will donate part of their

3 lines (2 loc) 3.32 kB
import{P as n,b as m,a as l}from"../chunks/promo-types-DKAOFHJr.esm.js";import{L as u}from"../chunks/beam-errors-Ci0d3926.esm.js";import{setCookieValue as f,getCookieValue as c}from"./cookies.esm.js";import{createScopedLocalStorage as p}from"./local-storage.esm.js";import{logger as s}from"./logger.esm.js";const S=100,h="utm_",A="discount_code",O=()=>{const o=c(l);if(o)try{return JSON.parse(o)}catch(e){s.error("Failed to parse beam_promo_codes cookie:",e);return}},w=({validatedPromoCodes:o,domain:e})=>{const r={validatedPromoCodes:o},a=Date.now()+1e3*60*60*24*90;f({name:l,value:JSON.stringify(r),path:"/",domain:e,expires:a})},C=({apiKey:o})=>o?p({apiKey:o}).getItemJson(n)??{unvalidatedPromoCodes:[],validatedPromoCodes:[]}:(s.error(new u("API key is required to get promo codes")),null),P=({apiKey:o,promoCodes:e})=>{if(!o){s.error(new u("API key is required to set promo codes"));return}const r=p({apiKey:o}),a={unvalidatedPromoCodes:e?.unvalidatedPromoCodes||[],validatedPromoCodes:e?.validatedPromoCodes||[]};r.setItemJson(n,a)},v=({newPromos:o,apiKey:e})=>{const r=C({apiKey:e})??{unvalidatedPromoCodes:[],validatedPromoCodes:[]},a=(o?.unvalidatedPromoCodes||[]).filter(d=>!r.unvalidatedPromoCodes.some(i=>JSON.stringify(i)===JSON.stringify(d))),t={unvalidatedPromoCodes:[...r.unvalidatedPromoCodes,...a],validatedPromoCodes:[...r.validatedPromoCodes,...o?.validatedPromoCodes||[]]};return(t.unvalidatedPromoCodes.length>0||t.validatedPromoCodes.length>0)&&P({apiKey:e,promoCodes:t}),{...t}},g=o=>o.reduce((e,r)=>{const a=e||[];return r.url?a.push({type:m.Url,attributes:{url:r.url}}):r.value?a.push({type:m.Manual,attributes:{value:r.value}}):s.error("Invalid PromoAttribute - must contain either 'url' or 'value'",{attr:r}),a},[]),K=(o,e)=>{const r=g(o);return v({newPromos:{unvalidatedPromoCodes:r},apiKey:e})},_=o=>{if(!o?.content)return[];const e=o.content.discounts?.filter(t=>t.applicable)?.map(t=>t.code)||[],r=o.content.items?.flatMap(t=>t.discounts?.filter(d=>d.applicable)?.map(d=>d.code)||[])||[],a=[...e,...r];return[...new Set(a)]},b=()=>c(A)??null,E=o=>{if(!o)return[];try{const e=JSON.parse(o);return Array.isArray(e)&&e.every(r=>typeof r=="string")?e:[]}catch(e){return s.error("Failed to parse JSON:",e),[]}},J=()=>{try{const o=new URL(window.location.href);if(!o.search)return;const e=new URL(o.origin+o.pathname);let r=!1;o.searchParams.forEach((t,d)=>{const i=d.startsWith(h),y=t.length<=S;i&&y&&(e.searchParams.append(d,t),r=!0)});const a=e.toString();return r?{url:a}:void 0}catch{return}},L=(o,e)=>{const r=a=>(Array.isArray(a)?a:[]).map(t=>t.type==="manual"?t.attributes?.value:t.type==="url"?t.attributes?.url:"").filter(Boolean).sort().join(",");return r(o?.unvalidatedPromoCodes)===r(e?.unvalidatedPromoCodes)},k=o=>(!o.unvalidatedPromoCodes||o.unvalidatedPromoCodes.length===0)&&(!o.validatedPromoCodes||o.validatedPromoCodes.length===0);export{K as formatStoreAndReturnPromoCodes,O as getAllPromoCodesFromCookie,b as getPromoCodeFromShopifyCookie,_ as getPromoCodesFromCart,C as getPromoCodesLocalStorage,J as getSanitizedUrlForStorage,k as isEmptyPromoData,g as mapToUnvalidatedPromoCodes,v as mergeStoreAndReturnPromos,E as parseJsonStringArray,L as promoCodesAreEqual,w as setPromoCodeInCookie,P as setPromoCodesInLocalStorage}; //# sourceMappingURL=promoManager.esm.js.map