"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const R=require("react/jsx-runtime"),i=require("react"),c=require("react-ga4"),x=require("react-router"),P=require("../conf/index.js"),b=require("@devgateway/wp-react-lib"),E=require("./internalTrafficUtils.js"),C=new Set;let s=!1;const L=l=>{const f=T=>{var m;const d=i.useContext(b.SettingsContext)??{},e=((m=d==null?void 0:d.data)==null?void 0:m.google_analytics_code)??P.Config.GA_CODE,a=x.useLocation(),_=i.useRef(null),u=i.useRef(!1),g=i.useRef(!1);if(typeof window<"u"&&!s&&e&&e!=="#REACT_APP_GA_CODE#"){if(!C.has(e))try{c.initialize(e),C.add(e),console.log("GA initialized with code:",e)}catch(t){console.error("Failed to initialize GA:",t)}s=!0}return i.useEffect(()=>{if(typeof window>"u"||!e||e==="#REACT_APP_GA_CODE#"||!s)return;const t=a.pathname;if(_.current!==t){_.current=t,u.current=!1;try{const r=E.isInternalTrafficEnabled()?"internal":"external";c.event("page_view",{page_path:t,traffic_type:r}),console.log("GA pageview sent:",{page:t,traffic_type:r})}catch(r){console.error("Failed to send pageview:",r)}}},[a.pathname,e]),i.useEffect(()=>{if(typeof window>"u"||!e||e==="#REACT_APP_GA_CODE#"||!s||g.current)return;const t=E.isInternalTrafficEnabled()?"internal":"external",r=()=>{if(u.current)return;const n=Math.round(window.scrollY/(document.documentElement.scrollHeight-window.innerHeight)*100);if(n>10)try{c.event("scroll",{engagement_type:"scroll",scroll_depth:n,traffic_type:t}),console.log("GA scroll event sent:",{scrollPercentage:n,traffic_type:t}),u.current=!0}catch(o){console.error("Failed to send scroll event:",o)}},p=n=>{var A;const o=n.target,h=((A=o.textContent)==null?void 0:A.substring(0,50))||"",v=o.id||"",y=o.className||"";if(!(!h&&!v&&!y))try{c.event("element_click",{element_id:v,element_class:y,element_text:h.substring(0,50),traffic_type:t})}catch{}},G=Date.now(),w=()=>{const n=Math.round((Date.now()-G)/1e3);if(n>3)try{c.event("time_on_page",{page_path:a.pathname,duration_seconds:n,traffic_type:t}),console.log("GA time_on_page event sent:",{timeOnPage:n,path:a.pathname})}catch(o){console.error("Failed to send time_on_page event:",o)}};return window.addEventListener("scroll",r,{passive:!0}),document.addEventListener("click",p),window.addEventListener("beforeunload",w),g.current=!0,()=>{window.removeEventListener("scroll",r),document.removeEventListener("click",p),window.removeEventListener("beforeunload",w)}},[a.pathname,e]),R.jsx(l,{...T})};return f.displayName=`withTracker(${l.displayName||l.name||"Component"})`,f};exports.withTracker=L;
