"use client";
import { jsx } from "react/jsx-runtime";
import { useContext, useRef, useState, useEffect } from "react";
import Cookies from "universal-cookie";
import { App, AnalyticsContext } from "./AnalyticsContext.js";
import { useIsomorphicLayoutEffect } from "../hooks/useIsomorphicLayoutEffect.js";
import { GDSContext } from "../theme/GDSContext.js";
const serializeConsent = (consent) => consent ? "true" : "false";
const deserializeConsent = (consent) => {
  switch (consent) {
    case "true":
      return true;
    case "false":
      return false;
    default:
      return null;
  }
};
const AnalyticsProvider = ({
  app: appKey,
  mixpanel: mixpanelProp,
  googleAnalytics: googleAnalyticsProp,
  children
}) => {
  const { isProvided, useRouter } = useContext(GDSContext);
  if (!isProvided) {
    throw new Error("AnalyticsProvider must be wrapped in GDSProvider");
  }
  const app = App[appKey];
  const appString = app;
  const cookies = useRef(null);
  const [, setForceRerender] = useState({});
  const forceRerender = () => setForceRerender({});
  if (!cookies.current) {
    cookies.current = new Cookies();
  }
  const hasConsentCookie = deserializeConsent(
    cookies.current.get("cookie-consent", {
      doNotParse: true
      // we handle deserialization ourselves, for consistency with `localStorage`
    })
  );
  const setConsentCookie = (consent) => {
    var _a;
    (_a = cookies.current) == null ? void 0 : _a.set("cookie-consent", serializeConsent(consent), {
      domain: ".thegraph.com",
      // include all subdomains
      path: "/",
      // include all paths
      maxAge: 60 * 60 * 24 * 365 * 10,
      // 10 years
      sameSite: "lax"
    });
  };
  const hasConsentSession = typeof localStorage !== "undefined" ? deserializeConsent(localStorage.getItem("cookie-consent")) : null;
  const setConsentSession = (consent) => {
    if (typeof localStorage !== "undefined") {
      localStorage.setItem("cookie-consent", serializeConsent(consent));
    }
  };
  const hasConsent = hasConsentSession ?? hasConsentCookie;
  const setConsent = (consent) => {
    setConsentSession(consent);
    setConsentCookie(consent);
    forceRerender();
  };
  const mixpanel = mixpanelProp ? mixpanelProp.sdk : null;
  const mixpanelToken = (mixpanelProp == null ? void 0 : mixpanelProp.token) ?? null;
  const googleAnalytics = googleAnalyticsProp && hasConsent ? googleAnalyticsProp.sdk : null;
  const googleAnalyticsMeasurementId = (googleAnalyticsProp == null ? void 0 : googleAnalyticsProp.measurementId) ?? null;
  const mixpanelInitialized = useRef(false);
  useIsomorphicLayoutEffect(() => {
    if (mixpanel && !mixpanelInitialized.current) {
      mixpanel.init(mixpanelToken || "BOGUS_TOKEN", {
        debug: process.env.NODE_ENV !== "production",
        persistence: "localStorage"
      });
      mixpanel.register(
        {
          app: appString
        },
        { persistent: false }
      );
      mixpanelInitialized.current = true;
    }
    if (googleAnalytics && !googleAnalytics.isInitialized && googleAnalyticsMeasurementId) {
      googleAnalytics.initialize(googleAnalyticsMeasurementId);
    }
  }, [mixpanel, mixpanelToken, googleAnalytics, googleAnalyticsMeasurementId, appString]);
  const router = useRouter();
  const path = router == null ? void 0 : router.fullPathname;
  useEffect(() => {
    if (!mixpanel || !mixpanelInitialized.current || !path) return;
    mixpanel.track("Viewed Page", {
      path
    });
  }, [mixpanel, path]);
  return /* @__PURE__ */ jsx(
    AnalyticsContext.Provider,
    {
      value: {
        app,
        mixpanel,
        googleAnalytics,
        hasConsent,
        setConsent
      },
      children
    }
  );
};
export {
  AnalyticsProvider
};
