"use client";

import { GoogleTagManager } from "@next/third-parties/google";
import { usePathname } from "next/navigation";

import CookieFirstScript from "@/app/cookie-management/script-cookiefirst";
import HotjarScript from "@/app/hotjar/script-hotjar";
import MetaPixel from "@/app/meta-pixel/script-meta";

const CLIENT_PREVIEW_PATH = "/participant-list/preview";

type TelemetryBootstrapProps = {
  cookieFirstKey: string;
  currentEnv: string;
  gtmId: string;
  hotjarSiteId: string;
};

export default function TelemetryBootstrap({
  cookieFirstKey,
  currentEnv,
  gtmId,
  hotjarSiteId,
}: TelemetryBootstrapProps) {
  const pathname = usePathname();
  if (
    pathname === CLIENT_PREVIEW_PATH ||
    pathname.startsWith(`${CLIENT_PREVIEW_PATH}/`)
  ) {
    return null;
  }

  return (
    <>
      {currentEnv !== "development" && cookieFirstKey && (
        <CookieFirstScript siteKey={cookieFirstKey} />
      )}
      {currentEnv !== "development" && hotjarSiteId && (
        <HotjarScript siteId={hotjarSiteId} />
      )}
      {currentEnv !== "development" && <GoogleTagManager gtmId={gtmId} />}
      <MetaPixel />
    </>
  );
}
