setTimeout(_,y))}console.error("Max retries reached. Unable to fetch data?.")}async function k({user:_}){try{await fetch(`${t}/v1/getcustomer?shop=${e}`,{headers:{"Content-Type":"application/json"},method:"PATCH",body:JSON.stringify({email:_?.email,data:{widgetSignup:!0}})})}catch($){return console.error(`Error in API request: ${$.message}`),null}}function L(_){let t=a.hash,$=_.replace("#","");t.split("_")[1];let e="rediem_referral"!=t.replace("#","")&&window.location.hash.includes($);e&&w(p,C)}function f(){w(p,C)}function g(_){_.preventDefault(),f()}h.querySelector("#rippleRewardBtnClose")?.addEventListener("click",function _(){let t=document.querySelector(".ripple-frame-container"),$=document.querySelector("#rippleFect-container"),e=document.querySelector("body");t.classList.remove("show"),$.classList.remove("widget-open"),e.classList.remove("rippl-widget--open")}),C.addEventListener("click",function(){w(p,C)});let m=6,y=1500;async function M({products:_,subscription:t=!1}){let $=await x(),e=sessionStorage.getItem("opt_type");try{let r=t?T():F();if(!r)return;if(r?.requiredPaidProduct){let a=S({cartItems:$,freeProducts:r});if(!a)return}if(r?.perCartLimit){let o=function _({cartItems:t,freeProducts:$}){let e=t?.filter(_=>$?.productData?.find(t=>t?.varientId==_.variant_id));if(e?.length>0)throw Error("You can only add one free product!");return!1}({cartItems:$,freeProducts:r});if(o)return}let i=$.map(_=>_.variant_id),s=_.filter(_=>!i.includes(_.variantId));if(0===s.length)throw Error("Product already in cart!");let d=s.map(async _=>{let t=new URLSearchParams,$=_.variantId;t.append("id",$),t.append("quantity",_.quantity);let e=await fetch("/cart/add.js",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:t.toString()});if(!e.ok){let r=await e.json();throw Error(`${r?.message}: ${r?.description}`)}return e});await Promise.all(d),$=await x();let n=$?.find(_=>_.discounts.length>0)?.discounts[0].title??null,c=r?.perCartLimit?r?.productData.filter(t=>_.some(_=>_.variantId===t.varientId)):r?.productData.filter(_=>$.some(t=>t.variant_id===_.varientId)),l=r?.productDocId,h="addProductToCartSubscription"==e?c.map(_=>_.docId):c.map(_=>_.productDocId),p=t?await j({freeProductsData:r,productDocIds:h,couponCode:n}):await Z({freeProductsData:r,productDocIds:h,couponCode:n,currProdId:l});p?window.location.href="/cart":(I({message:"Failed to generate coupon",items:$}),console.error("Failed to generate coupon"))}catch(C){console.error(C.message),I({message:C?.message??"Failed to add product to cart!",items:$})}}async function v({products:_}){try{let t=_.map(async _=>{let t=new URLSearchParams,$=_.product?.split("/")?.at(-1);t.append("id",$),t.append("quantity",1);let e=await fetch("/cart/add.js",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:t.toString()});if(!e.ok){let r=await e.json();throw Error(`${r?.message}: ${r?.description}`)}return e});await Promise.all(t),window.location.href="/cart"}catch($){console.error($.message),I({message:$?.message??"Failed to add product to cart!"})}}async function b({cartJson:_,freeProduct:t}){let $=[],e=_?.items?.filter(_=>t?.productData?.find(t=>t?.varientId==_.variant_id));e?.length>0&&e.forEach(_=>{_.quantity>1&&$.push({id:_.variant_id,quantity:1})}),$.length>0&&(alert("Only single quantity is allowed!"),await Promise.all($.map(_=>{let t=new URLSearchParams;return t.append("id",_.id),t.append("quantity",_.quantity),fetch("/cart/change.js",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:t.toString()})})),location.reload())}!async function(){if(e&&!d)try{let _=await fetch(`${t}/v1/getsettings?shopDomain=${e}`,{headers:{"Content-Type":"application/json"},method:"GET"}),$=await _?.json();if($?.data){let a=$?.data,{launchText:o,launchTextGuest:n,widgetIcon:c,presetIconKey:l,launchButtonColor:w,widgetPlacement:m,sideSpacing:y,launchButtonRadius:M,launchIconPlacement:v="top_left",widgetType:b,primaryColor:V,cornerRadius:x,showWidgetIcon:H,bottomSpacing:I,launchButtonIndex:S=50,fontFamily:F,fontFamilyLink:T,iconAppearance:Z}=a||{},j=C.querySelector(".btn-text"),P=h.querySelector(".ripple-frame-container"),E=h.querySelector(".launchUIButton"),D=h.querySelector("#rippleRewardBtnClose"),B=o?.trim().length?o:a?.programName,R=n?.trim().length?n:B,A=c?`

`:function _(t,$){switch(t){case"icon1":return``;case"icon2":return``;case"icon3":return``;case"icon4":return` `;case"icon5":return``;case"icon6":return``;default:return``}}(l,w),O=`${m}: ${y}px;`;if(P.style.cssText=O,C.style.cssText=O,C.style.borderRadius=`${M}px`,C.style.bottom=`${I}px`,C.style.zIndex=S,C.style.background=w,P.style.borderRadius=`${x}px`,P.style.bottom="30px",P.style.height="calc(100vh - 80px)",E.classList.add(v),E.innerHTML=A,p.classList.add(b),p.classList.add(`position-${m}`),C.classList.add(H?"showWidgetIcon":"hash"),C.classList.add(Z),D.querySelector("svg").setAttribute("stroke",V),j.innerText=r?B:R,T&&T.startsWith("https://")){let z=document.createElement("style");z.textContent=`@import url(${T});`,document.head.appendChild(z),C.style.fontFamily=F}if(r&&!d&&a?.programStatus=="active"){let U=await u(r);if(U?.data&&(U?.data?.status==="active"||U?.data?.status==="delete")){if(!(a?.programStatus==="active"&&!a?.finishLater))return null;if(document.body.appendChild(h),L(a?.embedLink??null),a?.tierStatus==="active"&&U?.data?.freeShipping&&U?.data?.couponCode){var W=U?.data?.couponCode;W&&(document.cookie=`discount_code=${W}; path=/`,fetch(`/discount/${W}`).then(function(){return fetch("/cart/update.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({updates:{}})})}).catch(function(_){console.error("Error:",_)}))}let N=JSON.parse(sessionStorage.getItem("ripplRewardData"));N&&N?.rdashtrigger&&(f(),U?.data?.widgetSignup||k({user:U?.data}),sessionStorage.removeItem("ripplRewardData"));let G=document.querySelectorAll(`a[href="${a?.embedLink}"]`);G&&G.length>0&&G.forEach(_=>{_.addEventListener("click",g)}),d=!0}}else if(s&&i==a?.uniqueCode)document.body.appendChild(h);else{if(!(a?.programStatus=="active"&&!a?.finishLater))return null;document.body.appendChild(h),L(a?.embedLink??null);let J=document.querySelectorAll(`a[href="${a?.embedLink}"]`);J&&J.length>0&&J.forEach(_=>{_.addEventListener("click",g)}),d=!0}a?.programStatus=="active"&&d&&q(e)}}catch(Y){console.error(Y)}}(),window.addEventListener("message",_=>{if("openRipplUrl"===_.data.type&&window.open(_.data.url,"_blank"),"ripplRewardSaveData"===_.data.type&&sessionStorage.setItem("ripplRewardData",JSON.stringify(_.data.data)),"addProductToCart"===_.data.type&&(sessionStorage.setItem("freeProducts",JSON.stringify(_.data?.data)),sessionStorage.setItem("opt_type",_.data.type),M({products:_.data.products})),"addProductToCartSubscription"===_.data.type&&(sessionStorage.setItem("freeProductsSubscription",JSON.stringify(_.data?.data)),sessionStorage.setItem("opt_type",_.data.type),M({products:_.data.products,subscription:!0})),"exclusiveProduct"===_.data.type&&v({products:[_.data.item]}),"trigger_wiget_open"===_.data.type&&w(p,C,!0),"access_session_data"===_.data.type){let t=_.data.key,$=JSON.parse(sessionStorage.getItem(t)??"{}");if("link_actions"===t){let e=window.location.href,r=e?.endsWith("/")?e.slice(0,-1):e,a=$?.trackingLink?.endsWith("/")?$?.trackingLink?.slice(0,-1):$?.trackingLink;r===a&&I($,t)}else I($,t)}});let V=window.fetch;async function x(){let _=await fetch("/cart.js");if(!_.ok)throw Error("Failed to fetch cart contents");let t=await _.json();return t.items}async function H(_){try{for(let t of _){let $=new URLSearchParams;$.append("id",t.id),$.append("quantity",0);let e=await fetch("/cart/change.js",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:$.toString()});e.ok||console.error("Error removing item:",t.id)}location.reload()}catch(r){console.error("Error removing items:",r)}}function I(t,$="cartUpdated"){let e=document.getElementById("RippleRewards"),r={type:$,data:t,..."cartUpdated"===$&&{cart:t}};e.contentWindow.postMessage(r,_)}function S({cartItems:_,freeProducts:t,error:$=!0}){let e=_?.filter(_=>t?.productData?.find(t=>t?.varientId!=_.variant_id));if(e?.length<1){if($)throw Error("To redeem a free product, you must add a paid product to cart!");H(_)}return!0}function F(){let _=sessionStorage.getItem("freeProducts");if(!_)throw Error("Something went wrong!");return JSON.parse(_)}function T(){let _=sessionStorage.getItem("freeProductsSubscription");if(!_)throw Error("Something went wrong!");return JSON.parse(_)}async function Z({freeProductsData:_,productDocIds:t,couponCode:e,currProdId:r}){let a=new Headers;a.append("Authorization",`Bearer ${_?.token}`),a.append("Content-Type","application/json");let o={method:"POST",headers:a,body:JSON.stringify({actionDocId:_?.actionDocId,productDocId:t,couponCode:e,productId:r}),redirect:"follow"};try{let i=await fetch(`${$}/waysToRedeem/free_product/`,o);if(!i.ok)return console.error("Failed to generate coupon:",i.statusText),!1;{let s=await i.json();return await E(s.coupon),!0}}catch(d){return console.error("Error generating coupon:",d),!1}}async function j({freeProductsData:_,couponCode:t}){let e=new Headers;e.append("Authorization",`Bearer ${_?.token}`),e.append("Content-Type","application/json");let r={method:"POST",headers:e,body:JSON.stringify({id:_?.docId,couponCode:t}),redirect:"follow"};try{let a=await fetch(`${$}/waysToEarn/subscription/`,r);if(!a.ok)return console.error("Failed to generate coupon:",a.statusText),!1;{let o=await a.json();return await E(o?.data?.couponCode),!0}}catch(i){console.error("Error generating coupon:",i)}}async function P(_){if(_){document.cookie=`discount_code=${_}; path=/; SameSite=Lax`;try{await fetch(`/discount/${_}`),window.location.reload()}catch(t){console.error("Error applying discount:",t)}}}async function q(_){try{let $=await fetch(`${t}/public/action/type?shop=${_}&type=6`);if(!$.ok)return console.error("Failed to data:",$),!1;{let e=await $.json(),r=window.location.href,a=r?.endsWith("/")?r.slice(0,-1):r,o=e?.data?.find(_=>{let t=_?.trackingLink?.endsWith("/")?_?.trackingLink.slice(0,-1):_?.trackingLink;return t===a});o&&sessionStorage.setItem("link_actions",JSON.stringify(o))}}catch(i){return console.error("Error getting actions:",i),!1}}async function E(_){if(_){sessionStorage.setItem("discount_code",_);try{await fetch(`/discount/${_}`)}catch(t){console.error("Error applying discount:",t)}}}async function D(){let _=sessionStorage.getItem("discount_code");if(_)try{let t=await fetch("/cart.js"),$=await t.json(),e=$.items.some(t=>t.discounts?.some(t=>t.title===_));e||await fetch(`/discount/${_}`)}catch(r){console.error("Error applying stored discount:",r)}}async function B(_){try{let $=await fetch(`${t}/activity?shop=${e}&customerId=${r}`,{method:"POST",body:JSON.stringify(_)});$.ok||console.error("Activity tracking failed:",$.statusText)}catch(a){console.error({activityError:a})}}window.fetch=async function(..._){try{let t=await V.apply(this,_),$=_?.[0]??t?.url;if($&&($.includes("/cart/add.js")||$.includes("/cart/update.js")||$.includes("/cart/change.js")||$.includes("/cart/clear.js")||$.includes("/cart/add")||$.includes("/cart/update")||$.includes("/cart/change")||$.includes("/cart/clear")))try{let e=t.clone(),r=await e.json();if(r?.items?.length>0){let a=sessionStorage.getItem("opt_type");if(a){let o="addProductToCart"===a?F():"addProductToCartSubscription"===a?T():null;o?.requiredPaidProduct&&S({cartItems:r?.items,freeProducts:o,error:!1}),o?.perCartLimit&&await b({cartJson:r,freeProduct:o})}}}catch(i){console.error("Error in custom cart handling:",i)}return t}catch(s){throw console.error("Fetch error:",s),s}},document.addEventListener("DOMContentLoaded",D)}();