// @bun
var{defineProperty:N,getOwnPropertyNames:V,getOwnPropertyDescriptor:W}=Object,X=Object.prototype.hasOwnProperty;function Y(q){return this[q]}var v=(q)=>{var j=(O??=new WeakMap).get(q),w;if(j)return j;if(j=N({},"__esModule",{value:!0}),q&&typeof q==="object"||typeof q==="function"){for(var z of V(q))if(!X.call(j,z))N(j,z,{get:Y.bind(q,z),enumerable:!(w=W(q,z))||w.enumerable})}return O.set(q,j),j},O;var Z=(q)=>q;function _(q,j){this[q]=Z.bind(null,j)}var C=(q,j)=>{for(var w in j)N(q,w,{get:j[w],enumerable:!0,configurable:!0,set:_.bind(j,w)})};var $=(q,j)=>()=>(q&&(j=q(q=0)),j);var T=import.meta.require;function Q(q){let j=q.analytics;if(!j?.enabled)return"";switch(j.driver){case"fathom":return D(j);case"google-analytics":return E(j);case"plausible":return F(j);case"self-hosted":return G(j);case"custom":return M(j);default:return""}}function D(q){let j=q.fathom;if(!j?.siteId)return"";let w=j.scriptUrl||"https://cdn.usefathom.com/script.js",z=j.defer!==!1?" defer":"",J=[`data-site="${K(j.siteId)}"`];if(j.honorDnt)J.push('data-honor-dnt="true"');if(j.spa)J.push('data-spa="auto"');if(j.canonical)J.push(`data-canonical="${K(j.canonical)}"`);if(j.auto===!1)J.push('data-auto="false"');return`
<!-- Fathom Analytics -->
<script src="${K(w)}" ${J.join(" ")}${z}></script>
`}function E(q){let j=q.googleAnalytics;if(!j?.measurementId)return"";let w=K(j.measurementId),z=j.debug?`gtag('config', '${w}', { 'debug_mode': true });`:"";return`
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=${w}"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', '${w}');
  ${z}
</script>
`}function F(q){let j=q.plausible;if(!j?.domain)return"";let w=j.scriptUrl||"https://plausible.io/js/script.js",z=[`data-domain="${K(j.domain)}"`],J=w,L=[];if(j.trackLocalhost)L.push("local");if(j.hashMode)L.push("hash");if(L.length>0&&w==="https://plausible.io/js/script.js")J=`https://plausible.io/js/script.${L.join(".")}.js`;return`
<!-- Plausible Analytics -->
<script defer data-domain="${K(j.domain)}" src="${K(J)}"></script>
`}function G(q){let j=q.selfHosted;if(!j?.siteId||!j?.apiEndpoint)return"";let w=K(j.siteId),z=K(j.apiEndpoint),J=j.honorDnt?'if(n.doNotTrack==="1")return;':"",L=j.trackHashChanges?"w.addEventListener('hashchange',pv);":"",R=j.trackOutboundLinks?`
  d.addEventListener('click',function(e){
    var a=e.target.closest('a');
    if(a&&a.hostname!==location.hostname){
      t('outbound',{url:a.href});
    }
  });`:"";return`
<!-- Self-Hosted Analytics -->
<script data-site="${w}" data-api="${z}" defer>
(function(){
  'use strict';
  var d=document,w=window,n=navigator,s=d.currentScript;
  var site=s.dataset.site,api=s.dataset.api;
  ${J}
  var q=[],sid=Math.random().toString(36).slice(2);
  function t(e,p){
    var x=new XMLHttpRequest();
    x.open('POST',api+'/collect',true);
    x.setRequestHeader('Content-Type','application/json');
    x.send(JSON.stringify({
      s:site,sid:sid,e:e,p:p||{},
      u:location.href,r:d.referrer,t:d.title,
      sw:screen.width,sh:screen.height
    }));
  }
  function pv(){t('pageview');}
  ${L}
  ${R}
  if(d.readyState==='complete')pv();
  else w.addEventListener('load',pv);
  w.stxAnalytics={track:function(n,v){t('event',{name:n,value:v});}};
})();
</script>
`}function M(q){let j=q.custom;if(!j)return"";if(j.inlineScript)return`
<!-- Custom Analytics -->
<script>
${j.inlineScript}
</script>
`;if(!j.scriptUrl)return"";let w=[];if(j.scriptId)w.push(`id="${K(j.scriptId)}"`);if(j.attributes)for(let[J,L]of Object.entries(j.attributes))w.push(`${K(J)}="${K(L)}"`);return`
<!-- Custom Analytics -->
<script${w.length>0?` ${w.join(" ")}`:""} src="${K(j.scriptUrl)}" defer></script>
`}function B(q,j){let w=Q(j);if(!w)return q;if(!q.includes("</head>")){let z=q.lastIndexOf("</body>");if(z!==-1)return q.slice(0,z)+w+q.slice(z);return q+w}return q.replace("</head>",`${w}</head>`)}function K(q){return q.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}var U;var P=$(()=>{U={name:"analytics",hasEndTag:!1,handler:async(q,j,w,z)=>{let J=w.__stx_config||{};return Q(J)}}});P();export{B as injectAnalytics,Q as generateAnalyticsScript,U as analyticsDirective};
