// instrumentation-client.js
import posthog from "posthog-js";

// envs lesen
const key = process.env.NEXT_PUBLIC_POSTHOG_KEY;
const host = process.env.NEXT_PUBLIC_POSTHOG_HOST || "https://us.i.posthog.com";

// nur im Browser initialisieren + Doppel-Init verhindern
if (typeof window !== "undefined" && !window.__posthogInitialized) {
  if (key) {
    posthog.init(key, {
      api_host: host,
      capture_pageview: false, // Pageviews trackst du selbst
      loaded: () => {
        if (process.env.NODE_ENV === "development") posthog.debug();
      },
    });
    window.__posthogInitialized = true;
  } else {
    console.warn("NEXT_PUBLIC_POSTHOG_KEY ist nicht gesetzt");
  }
}

export default posthog;
