(()=>{"use strict";const t="https://fgfrztymfdbzylchdjps.supabase.co",e="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZnZnJ6dHltZmRienlsY2hkanBzIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MTM4NjY0NDAsImV4cCI6MjAyOTQ0MjQ0MH0.xZBbPdEugwctsPjL90vRZ1-kn8aYTobn4RWo93lPib0",n="https://api.gleef.eu/update-success",o=36e5,s=o,r=o,c=o;var i;async function a(n,o={}){const s=await fetch(`${t}/rest/v1/${n}`,{body:o.body,method:o.method,headers:{"Content-Type":"application/json",apiKey:e,...o.headers}}),r=await s.text().catch((()=>null));if(!s.ok){const t=r?JSON.parse(r):{message:"Unknown error"};throw new Error(t.message)}return r?JSON.parse(r):null}!function(t){t.URL="page_view",t.ELEMENT_CLICK="element_click"}(i||(i={}));async function l(t,e){if(function(){const t=localStorage.getItem("lastVisit"),e=t?JSON.parse(t):null;return!e||Date.now()-e>=r}())try{await async function(t,e){await a("variation_shown",{method:"POST",body:JSON.stringify({variation_id:t,visitor_id:e})})}(t.id,e)}catch(t){console.error("Failed to send visit",t)}finally{localStorage.setItem("lastVisit",Date.now().toString())}}async function u(t,e){const o={method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({success_event_id:t,visitor_id:e})};try{const t=await fetch(n,o);await async function(t){if(!t.ok){const e=await t.text();throw new Error(`Gleef API error: ${e}`)}}(t)}catch(t){throw console.error("Failed to send success event",t),t}}function d(t){return t=t.replace(/^(https?:\/\/)www\./,"$1").replace(/\?.*$/,"").replace(/\/$/,""),/^https?:\/\//.test(t)||(t="https://"+t),t}const f={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};let p;const h=new Uint8Array(16);function y(){if(!p&&(p="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!p))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return p(h)}const v=[];for(let t=0;t<256;++t)v.push((t+256).toString(16).slice(1));const m=function(t,e,n){if(f.randomUUID&&!e&&!t)return f.randomUUID();const o=(t=t||{}).random||(t.rng||y)();if(o[6]=15&o[6]|64,o[8]=63&o[8]|128,e){n=n||0;for(let t=0;t<16;++t)e[n+t]=o[t];return e}return function(t,e=0){return v[t[e+0]]+v[t[e+1]]+v[t[e+2]]+v[t[e+3]]+"-"+v[t[e+4]]+v[t[e+5]]+"-"+v[t[e+6]]+v[t[e+7]]+"-"+v[t[e+8]]+v[t[e+9]]+"-"+v[t[e+10]]+v[t[e+11]]+v[t[e+12]]+v[t[e+13]]+v[t[e+14]]+v[t[e+15]]}(o)};function w(t,e,n=document){const o=function(t,e){const n=[];return t.forEach((t=>{const{originalText:o,selectorType:s}=t,r=`//${s}[text()="${o}"]`;n.push({xPath:e.evaluate(r,e,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null),newText:t.newText})})),n}(t.elementVariations,n);o.forEach((n=>{for(let o=0;o<n.xPath.snapshotLength;o++){const s=n.xPath.snapshotItem(o);s&&g(s,n.newText,t.experiment.success_event,e)}}))}function g(t,e,n,o){t.textContent=e,t.classList.add("gleef"),n.event_type===i.ELEMENT_CLICK&&function(t,e,n){e.addEventListener("click",(async()=>{try{await u(t.id,n)}catch(t){console.error("Failed to send success event",t)}}))}(n,t,o)}let _=d(window.location.href),I=null;(async function t(){const e=JSON.parse(localStorage.getItem("variations")||"null"),n=JSON.parse(localStorage.getItem("watchedUrlEvents")||"null"),o=function(){const t=localStorage.getItem("gleefUID");if(t)return t;const e=m();return localStorage.setItem("gleefUID",e),e}();let r=e&&function(t,e){const n=t[e];if(n){const{variation:t,variationSetAt:e}=n;if(Date.now()-e<s)return t}}(e,_);if(r)w(r,o),await l(r,o);else try{r=await async function(t){const e=await a("rpc/get_random_variation",{method:"POST",body:`{"client_url":"${t}"}`});return e?{id:(n=e).variation_id,experiment:{id:n.experiment_id,url:n.experiment_url,success_event:{id:n.success_event_id,event_type:"page_view"==n.success_event_type?i.URL:i.ELEMENT_CLICK,url:n.success_url?n.success_url:void 0}},elementVariations:n.element_variations.map((t=>({newText:t.new_text,selectorType:t.selector_type,originalText:t.original_text})))}:void 0;var n}(_),r&&(await l(r,o),function(t,e,n,o){const s={...n};s[t]={variation:e,variationSetAt:Date.now()},localStorage.setItem("variations",JSON.stringify(s)),function(t,e){const n=t.experiment.success_event,o=n.url&&d(n.url);if(n.event_type===i.URL&&o){const t={...e},s=t[o]||[];s.push({successEvent:n,successEventSetAt:Date.now()}),t[o]=s,localStorage.setItem("watchedUrlEvents",JSON.stringify(t))}}(e,o??{})}(_,r,e,n))}catch(t){console.error("Failed to fetch variations",t)}I&&I.disconnect(),I=new MutationObserver(function(e,n){return o=>{const s=d(window.location.href);_!==s?(_=s,t().catch((t=>console.error("Failed to refresh Gleef script",t)))):o&&e&&w(e,n)}}(r,o)),I.observe(document,{attributes:!1,childList:!0,subtree:!0}),n&&async function(t,e,n){if(t){const o=t[e];if(o)for(const t of o)if(t&&Date.now()-t.successEventSetAt<c)try{await u(t.successEvent.id,n),localStorage.removeItem("watchedUrlEvents")}catch(t){console.error("Failed to send success event",t)}}}(n,_,o).catch((t=>console.error("Failed to send success event saved and pending for this URL",t)))})().catch((t=>console.error("Failed to execute Gleef script",t)))})();