"use strict";(()=>{function v(s){return Promise.all(Object.entries(s).map(async([e,i])=>[e,await i??""])).then(e=>Object.fromEntries(e))}function m(s){let e={"Content-Type":"application/json"};return{headers:e,async fetch(i,t,n){let r=`${s}${i}`,o,d=await v(e);return new Promise(a=>{let p=c=>{clearTimeout(o),fetch(r,{headers:d,method:"POST",body:JSON.stringify(t??{}),keepalive:!0,...n??{}}).then(async l=>{if(l.status===401)return null;if(l.status!==200&&l.status!==202)return f(c,a);let g=await l.text();if(!g)return a(null);a(g)}).catch(()=>f(c,a))};function f(c,l){if(c>1)return l(null);o=setTimeout(()=>{p(c+1)},Math.pow(2,c)*500)}p(0)})}}}var u=class{constructor(e){this.state={properties:{}};this.options=e,this.api=m(e.url??"https://api.openpanel.dev"),this.api.headers["openpanel-client-id"]=e.clientId,this.options.clientSecret&&(this.api.headers["openpanel-client-secret"]=this.options.clientSecret)}setProfileId(e){this.state.profileId=e}setProfile(e){this.setProfileId(e.profileId),this.api.fetch("/profile",{...e,properties:{...this.state.properties,...e.properties}})}increment(e,i,t){let n=t?.profileId??this.state.profileId;if(!n)return console.log("No profile id");this.api.fetch("/profile/increment",{profileId:n,property:e,value:i})}decrement(e,i,t){let n=t?.profileId??this.state.profileId;if(!n)return console.log("No profile id");this.api.fetch("/profile/decrement",{profileId:n,property:e,value:i})}event(e,i){let t=i?.profileId??this.state.profileId;delete i?.profileId,this.api.fetch("/event",{name:e,properties:{...this.state.properties,...i??{}},timestamp:this.timestamp(),deviceId:this.getDeviceId(),profileId:t}).then(n=>{this.options.setDeviceId&&n&&this.options.setDeviceId(n)})}setGlobalProperties(e){this.state.properties={...this.state.properties,...e}}clear(){this.state.deviceId=void 0,this.state.profileId=void 0,this.options.removeDeviceId&&this.options.removeDeviceId()}timestamp(){return new Date().toISOString()}getDeviceId(){if(this.state.deviceId)return this.state.deviceId;this.options.getDeviceId&&(this.state.deviceId=this.options.getDeviceId()||void 0)}};function b(s){return s.replace(/([-_][a-z])/gi,e=>e.toUpperCase().replace("-","").replace("_",""))}var h=class extends u{constructor(i){super(i);this.lastPath="";this.isServer()||(this.setGlobalProperties({__referrer:document.referrer}),this.options.trackOutgoingLinks&&this.trackOutgoingLinks(),this.options.trackScreenViews&&this.trackScreenViews(),this.options.trackAttributes&&this.trackAttributes())}isServer(){return typeof document>"u"}trackOutgoingLinks(){this.isServer()||document.addEventListener("click",i=>{let t=i.target,n=t.closest("a");if(n&&t){let r=n.getAttribute("href");r?.startsWith("http")&&super.event("link_out",{href:r,text:n.innerText||n.getAttribute("title")||t.getAttribute("alt")||t.getAttribute("title")})}})}trackScreenViews(){if(this.isServer())return;let i=history.pushState;history.pushState=function(...r){let o=i.apply(this,r);return window.dispatchEvent(new Event("pushstate")),window.dispatchEvent(new Event("locationchange")),o};let t=history.replaceState;history.replaceState=function(...r){let o=t.apply(this,r);return window.dispatchEvent(new Event("replacestate")),window.dispatchEvent(new Event("locationchange")),o},window.addEventListener("popstate",()=>window.dispatchEvent(new Event("locationchange"))),this.options.hash?window.addEventListener("hashchange",()=>this.screenView()):window.addEventListener("locationchange",()=>this.screenView()),setTimeout(()=>{this.screenView()},50)}trackAttributes(){this.isServer()||document.addEventListener("click",i=>{let t=i.target,n=t.closest("button"),r=t.closest("a"),o=n?.getAttribute("data-event")?n:r?.getAttribute("data-event")?r:null;if(o){let d={};for(let p of o.attributes)p.name.startsWith("data-")&&p.name!=="data-event"&&(d[b(p.name.replace(/^data-/,""))]=p.value);let a=o.getAttribute("data-event");a&&super.event(a,d)}})}screenView(i){if(this.isServer())return;let t=window.location.href;this.lastPath!==t&&(this.lastPath=t,super.event("screen_view",{...i??{},__path:t,__title:document.title}))}};(s=>{if(s.op&&"q"in s.op){let e=s.op.q||[],i=new h(e.shift()[1]);e.forEach(t=>{t[0]in i&&i[t[0]](...t.slice(1))}),s.op=(t,...n)=>{let r=i[t].bind(i);typeof r=="function"&&r(...n)}}})(window);})();
//# sourceMappingURL=cdn.global.js.map