let i=null,a=null,l=null,s=null;const u=typeof window>"u";typeof document<"u"&&(i=document.querySelector(`script[src^="${import.meta.url}"]`),a=i?.getAttribute("data-api")||i&&`${new URL(i.src).origin}/api/event`,l=i?.getAttribute("data-entity")||null,s=document.referrer);const d=t=>console.info(`[liwan]: ${t}`),c=t=>d(`Ignoring event: ${t}`),g=t=>{throw new Error(`Failed to send event: ${t}`)};async function p(t="pageview",e){const o=e?.endpoint||a;if(!o)return g("endpoint is required");if(localStorage?.getItem("disable-liwan"))return c("localStorage flag");if(/^localhost$|^127(?:\.\d+){0,2}\.\d+$|^\[::1?\]$/.test(location.hostname)||location.protocol==="file:")return c("localhost");const n=new URLSearchParams(location.search),r=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({name:t,entity_id:e?.entity||l,referrer:e?.referrer||s,url:e?.url||location.origin+location.pathname,utm:{campaign:n.get("utm_campaign"),content:n.get("utm_content"),medium:n.get("utm_medium"),source:n.get("utm_source"),term:n.get("utm_term")}})});r.ok||(d(`Failed to send event: ${r.statusText}`),g(r.statusText))}const m=()=>{window.__liwan_loaded=!0;let t;const e=()=>{t!==location.pathname&&(t=location.pathname,p("pageview"))};window.navigation?window.navigation.addEventListener("navigate",()=>e()):(window.history.pushState=new Proxy(window.history.pushState,{apply:(o,n,r)=>{Reflect.apply(o,n,r),e()}}),window.addEventListener("popstate",e),document.addEventListener("astro:page-load",()=>e())),e()};!u&&!window.__liwan_loaded&&i&&m();export{p as event};