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