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