import { jsx as m } from "react/jsx-runtime";
import d, { useRef as C, useEffect as c } from "react";
import s from "react-ga4";
import { useLocation as A } from "react-router";
import { Config as _ } from "../conf/index.js";
import { SettingsContext as u } from "@devgateway/wp-react-lib";
import { isInternalTrafficEnabled as g } from "./internalTrafficUtils.js";
const f = /* @__PURE__ */ new Set(), z = (p, h = {}) => (l) => {
  var n;
  const e = d.useContext(u) ?? {}, t = ((n = e == null ? void 0 : e.data) == null ? void 0 : n.google_analytics_code) ?? _.GA_CODE, i = A(), o = C(!1);
  return c(() => {
    t && t !== "#REACT_APP_GA_CODE#" && !o.current && (f.has(t) || (s.initialize(t), f.add(t), console.log("GA initialized with code:", t)), o.current = !0);
  }, [t]), c(() => {
    if (t && t !== "#REACT_APP_GA_CODE#" && o.current) {
      const r = i.pathname, a = g() ? "internal" : "external";
      s.send({
        hitType: "pageview",
        page: r,
        traffic_type: a
      }), console.log("GA pageview sent:", { page: r, traffic_type: a });
    }
  }, [i.pathname, t]), /* @__PURE__ */ m(p, { ...l });
};
export {
  z as withTracker
};
