// Crawlsight browser component. Put it once at the top of the app:
//
//   import { AgentAnalytics } from "agent-analytics/react";
//   <AgentAnalytics siteId="site_..." />
//
// It loads the tracker, which reads the signed meta tag the server kit adds to each page and reports how the visitor
// interacts (input physics, device, automation markers, route changes). Without the server kit it still runs, but
// its reports are unsigned and agents that never run JavaScript are not seen.
import { useEffect } from "react";

const DEFAULT_SRC = "https://agent-analytics.jacob-4fc.workers.dev/v1/t.js";

export function AgentAnalytics({ siteId, src = DEFAULT_SRC }: { siteId: string; src?: string }) {
  useEffect(() => {
    if (document.querySelector("script[data-agent-analytics]")) return;
    const s = document.createElement("script");
    s.src = src;
    s.async = true;
    s.dataset.agentAnalytics = "";
    s.dataset.site = siteId;
    document.head.appendChild(s);
  }, [siteId, src]);
  return null;
}
