"use client";
'use strict';

var posthog = require('posthog-js');
var react = require('posthog-js/react');
var react$1 = require('react');
var jsxRuntime = require('react/jsx-runtime');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var posthog__default = /*#__PURE__*/_interopDefault(posthog);

// 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 = undefined ?? {};
    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 } = react$1.useMemo(
    () => resolveConfig(propProjectKey, propApiHost),
    [propProjectKey, propApiHost]
  );
  const enabled = react$1.useMemo(() => {
    if (!projectKey) return false;
    if (typeof window === "undefined") return false;
    if (window.localStorage?.getItem("posthog_opt_out") === "true") return false;
    return true;
  }, [projectKey]);
  react$1.useEffect(() => {
    if (!enabled || !projectKey) return;
    posthog__default.default.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]);
  react$1.useEffect(() => {
    if (!enabled) return;
    if (user?.id) {
      posthog__default.default.identify(user.id, {
        email: user.email,
        name: user.name,
        role: user.role,
        organization_id: user.organizationId
      });
      if (user.organizationId) {
        posthog__default.default.group("organization", user.organizationId);
      }
    } else {
      posthog__default.default.reset();
    }
  }, [enabled, user?.id, user?.email, user?.name, user?.role, user?.organizationId]);
  if (!enabled) return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children });
  return /* @__PURE__ */ jsxRuntime.jsx(react.PostHogProvider, { client: posthog__default.default, children });
}
var useTelemetry = react.usePostHog;

exports.TelemetryProvider = TelemetryProvider;
exports.useTelemetry = useTelemetry;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map