"use client";
import posthog from 'posthog-js';
import { usePostHog, PostHogProvider } from 'posthog-js/react';
import { useMemo, useEffect } from 'react';
import { jsx, Fragment } from 'react/jsx-runtime';

// src/platform/telemetry/posthog-provider.tsx
var DEFAULT_API_HOST = "https://us.i.posthog.com";
function resolveConfig(propKey, propHost) {
  let key = propKey ?? "";
  let host = propHost ?? "";
  if (typeof window !== "undefined") {
    const runtime = window.__APP_CONFIG__?.posthog;
    if (!key && runtime?.key) key = runtime.key;
    if (!host && runtime?.host) host = runtime.host;
  }
  try {
    const env = import.meta.env ?? {};
    if (!key) key = env.VITE_POSTHOG_KEY ?? "";
    if (!host) host = env.VITE_POSTHOG_HOST ?? "";
  } catch {
  }
  if (!host) host = DEFAULT_API_HOST;
  return { key, host };
}
function TelemetryProvider({
  app,
  projectKey: propProjectKey,
  apiHost: propApiHost,
  recordSessions = true,
  user,
  children
}) {
  const { key: projectKey, host: apiHost } = useMemo(
    () => resolveConfig(propProjectKey, propApiHost),
    [propProjectKey, propApiHost]
  );
  const enabled = useMemo(() => {
    if (!projectKey) return false;
    if (typeof window === "undefined") return false;
    if (window.localStorage?.getItem("posthog_opt_out") === "true") return false;
    return true;
  }, [projectKey]);
  useEffect(() => {
    if (!enabled || !projectKey) return;
    posthog.init(projectKey, {
      api_host: apiHost,
      defaults: "2026-01-30",
      // Session replay: capture DOM + console + network (excluding
      // request/response bodies — PII risk). The masking config blanks
      // input fields by default; sensitive forms (login, MFA) should
      // mark themselves with `data-ph-no-capture` to be extra-safe.
      session_recording: recordSessions ? {
        maskAllInputs: true,
        recordCrossOriginIframes: false
      } : void 0,
      capture_pageview: "history_change",
      capture_pageleave: true,
      autocapture: true,
      // PostHog's default UA filter silently drops events from headless
      // browsers (incl. Playwright) — the SDK logs
      // "[WebExperiments] Refusing to render … likely bot" and never
      // POSTs to /e/. We want e2e test sessions visible in PostHog so
      // session replay can reproduce SPA bugs caught by Playwright. Real
      // crawler spam is already filtered at the CloudFront edge by WAF
      // rules; opting out here doesn't widen the spam surface in
      // practice for our deployed nonprod / prod hosts.
      opt_out_useragent_filter: true,
      // Always tag every event with the SPA slug so we can filter.
      loaded: (instance) => {
        instance.register({ app });
      }
    });
  }, [enabled, projectKey, apiHost, recordSessions, app]);
  useEffect(() => {
    if (!enabled) return;
    if (user?.id) {
      posthog.identify(user.id, {
        email: user.email,
        name: user.name,
        role: user.role,
        organization_id: user.organizationId
      });
      if (user.organizationId) {
        posthog.group("organization", user.organizationId);
      }
    } else {
      posthog.reset();
    }
  }, [enabled, user?.id, user?.email, user?.name, user?.role, user?.organizationId]);
  if (!enabled) return /* @__PURE__ */ jsx(Fragment, { children });
  return /* @__PURE__ */ jsx(PostHogProvider, { client: posthog, children });
}
var useTelemetry = usePostHog;

export { TelemetryProvider, useTelemetry };
//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map