File size: 27,019 Bytes
3d700dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/**
 * Telemetry service for tracking library usage.
 *
 * This module handles anonymous telemetry for the @openhands/agent-canvas package
 * using the PostHog SDK for reliable event delivery with batching, retry logic,
 * and offline support.
 *
 * TRACKING PHILOSOPHY:
 * - Install event (canvas_install): Sent immediately on first use, regardless of consent.
 *   This is anonymous and contains no PII - just basic browser info and a random ID.
 * - Session/custom events: Only sent after user grants consent via the consent modal.
 * - Users can opt out of all future tracking by declining consent.
 *
 * AD BLOCKER BYPASS:
 * By default, telemetry is routed through OpenHands' reverse proxy (z.openhands.dev)
 * to avoid being blocked by ad blockers. Library consumers can override this with:
 * - VITE_POSTHOG_HOST: Custom proxy URL or direct PostHog URL
 * - VITE_POSTHOG_UI_HOST: PostHog UI host (defaults to https://us.posthog.com)
 *
 * IMPORTANT: By default, telemetry is sent to the OpenHands PostHog project.
 * Source builds can override this with VITE_POSTHOG_API_KEY. Precompiled
 * library consumers can pass the same settings to configureTelemetry().
 *
 * Users can disable all telemetry (including install tracking) via:
 * - Setting VITE_DO_NOT_TRACK=1 environment variable
 * - Browser's Do Not Track setting
 * - Injecting window.__AGENT_CANVAS_DO_NOT_TRACK__ = true at runtime, which the
 *   static server does from AGENT_CANVAS_DISABLE_TELEMETRY=1 (or the equivalent
 *   --disable-telemetry flag) so a precompiled bundle can opt out without
 *   VITE_DO_NOT_TRACK baked into the image. Under this flag the PostHog client
 *   is never initialized, so consent mirrored from a backend cannot opt it in.
 */

import type { BootstrapConfig, CaptureResult, PostHog } from "posthog-js";
import { getLockedCloudAuthMode } from "#/api/agent-server-config";
import defaults from "../../config/defaults.json";

import packageJson from "../../package.json";
import {
  AGENT_CANVAS_CLIENT_SOURCE,
  AGENT_CANVAS_CLIENT_VERSION,
} from "#/api/client-source";
import {
  getBackendTelemetryProperties,
  getCloudTelemetryProperties,
  type BackendTelemetryContextInput,
  type CloudTelemetryContextInput,
} from "#/services/telemetry-context";

const TELEMETRY_CONSENT_KEY = "openhands-telemetry-consent";
const TELEMETRY_CONSENT_PENDING_CLOUD_SYNC_KEY =
  "openhands-telemetry-consent-pending-cloud-sync";
const TELEMETRY_CONSENT_PENDING_LOCAL_REVOCATION_KEY =
  "openhands-telemetry-consent-pending-local-revocation";
const TELEMETRY_CONSENT_CHANGE_EVENT = "openhands-telemetry-consent-change";
const TELEMETRY_FIRST_USE_KEY = "openhands-telemetry-first-use";
const TELEMETRY_SESSION_KEY = "openhands-telemetry-session";
const POSTHOG_INSTANCE_NAME = "agent-canvas";
const POSTHOG_PAGEVIEW_CAPTURE_MODE = "history_change";

// Unconfigured source builds use staging. Production release workflows pass
// VITE_POSTHOG_API_KEY explicitly for both the app and library artifacts.
const DEFAULT_POSTHOG_API_KEY: string =
  (import.meta.env.VITE_POSTHOG_API_KEY as string | undefined) ||
  defaults.telemetry.posthogApiKey;

// Default to OpenHands' reverse proxy to bypass ad blockers.
// The proxy at z.openhands.dev routes to PostHog's US region.
// Library consumers can override this with their own proxy or direct PostHog URL.
const DEFAULT_POSTHOG_HOST =
  import.meta.env.VITE_POSTHOG_HOST || "https://z.openhands.dev";

// UI host is needed for PostHog features like toolbar to work correctly
// when using a reverse proxy. Defaults to US region.
const DEFAULT_POSTHOG_UI_HOST =
  import.meta.env.VITE_POSTHOG_UI_HOST || "https://us.posthog.com";

export interface TelemetryConfig {
  /** PostHog project key. Useful for precompiled library consumers. */
  apiKey?: string;
  /** Event ingestion host or reverse proxy. */
  apiHost?: string;
  /** PostHog UI host used by toolbar links and other UI features. */
  uiHost?: string;
}

export type TelemetryConfiguration = TelemetryConfig | false;

export type TelemetryConsent = "granted" | "denied" | "pending";
export type ResolvedTelemetryConsent = Exclude<TelemetryConsent, "pending">;

export interface SetTelemetryConsentOptions {
  /** Do not persist a value mirrored from backend settings back to Cloud. */
  syncToCloud?: boolean;
}

let posthogInstance: PostHog | null = null;
let initializationPromise: Promise<PostHog | null> | null = null;
let pendingBootstrap: BootstrapConfig | undefined;
let telemetryConfig: TelemetryConfig = {};
let telemetryDisabled = false;

/** Deployment-level opt-out injected by static-server.mjs (see file header). */
function isRuntimeDoNotTrackEnabled(): boolean {
  return (
    typeof window !== "undefined" &&
    (window as unknown as Record<string, unknown>)
      .__AGENT_CANVAS_DO_NOT_TRACK__ === true
  );
}

/** True when telemetry must stay fully off: the SDK is never initialized. */
function isTelemetryHardDisabled(): boolean {
  return telemetryDisabled || isRuntimeDoNotTrackEnabled();
}

interface TelemetryIdentity {
  distinctId: string;
  properties: Record<string, unknown>;
}

// undefined means that Cloud identity has not resolved yet; null means that it
// resolved without a user. This distinction prevents startup from resetting a
// persisted identity while the current account is still loading.
let desiredTelemetryIdentity: TelemetryIdentity | null | undefined;
let desiredIdentityRevision = 0;
let appliedIdentityRevision = -1;

const CANVAS_EVENT_PROPERTIES = Object.freeze({
  client_source: AGENT_CANVAS_CLIENT_SOURCE,
  client_version: AGENT_CANVAS_CLIENT_VERSION,
  package_name: packageJson.name,
  package_version: packageJson.version,
});

let telemetryBackendContext = getBackendTelemetryProperties({});
let telemetryCloudContext = getCloudTelemetryProperties();

export function setTelemetryBackendContext(
  context: BackendTelemetryContextInput,
): void {
  telemetryBackendContext = getBackendTelemetryProperties(context);
}

export function setTelemetryCloudContext(
  context: CloudTelemetryContextInput | null,
): void {
  telemetryCloudContext = getCloudTelemetryProperties(context);
}

function addCanvasEventProperties(
  event: CaptureResult | null,
): CaptureResult | null {
  if (!event) return null;

  return {
    ...event,
    properties: {
      ...telemetryBackendContext,
      ...telemetryCloudContext,
      ...event.properties,
      ...CANVAS_EVENT_PROPERTIES,
    },
  };
}

function restorePostHogConsent(posthog: PostHog): void {
  if (telemetryDisabled || getTelemetryConsent() !== "granted") {
    posthog.opt_out_capturing();
  } else {
    posthog.opt_in_capturing();
  }
}

function resetPostHogIdentity(posthog: PostHog, resetDeviceId = false): void {
  posthog.reset(resetDeviceId);
  appliedIdentityRevision = -1;
  // PostHog reset clears its own consent persistence, so immediately restore
  // the canonical Canvas decision kept in localStorage.
  restorePostHogConsent(posthog);
}

function applyDesiredTelemetryIdentity(posthog: PostHog): void {
  if (desiredTelemetryIdentity === undefined || !isTelemetryEnabled()) return;

  const desiredId = desiredTelemetryIdentity?.distinctId;
  const currentId = posthog.get_property("$user_id");
  if (currentId != null && currentId !== desiredId) {
    resetPostHogIdentity(posthog);
  }

  if (desiredTelemetryIdentity === null) {
    appliedIdentityRevision = desiredIdentityRevision;
    return;
  }

  if (
    posthog.get_property("$user_id") !== desiredTelemetryIdentity.distinctId ||
    appliedIdentityRevision !== desiredIdentityRevision
  ) {
    posthog.identify(
      desiredTelemetryIdentity.distinctId,
      desiredTelemetryIdentity.properties,
    );
    appliedIdentityRevision = desiredIdentityRevision;
  }
}

function propertiesEqual(
  left: Record<string, unknown>,
  right: Record<string, unknown>,
): boolean {
  const keys = Object.keys(left);
  return (
    keys.length === Object.keys(right).length &&
    keys.every((key) => left[key] === right[key])
  );
}

/**
 * Configure the single Canvas telemetry client before its first use.
 * Passing false disables telemetry and install tracking for embedded hosts.
 */
export function configureTelemetry(config: TelemetryConfiguration): void {
  if (config === false) {
    if (telemetryDisabled) return;
    telemetryDisabled = true;
    posthogInstance?.opt_out_capturing();
    notifyTelemetryConsentListeners();
    return;
  }

  const wasDisabled = telemetryDisabled;
  telemetryDisabled = false;

  if (!posthogInstance && !initializationPromise) {
    const definedConfig = Object.fromEntries(
      Object.entries(config).filter(([, value]) => value !== undefined),
    ) as TelemetryConfig;
    telemetryConfig = { ...telemetryConfig, ...definedConfig };
  }

  if (wasDisabled) {
    if (posthogInstance) {
      restorePostHogConsent(posthogInstance);
      applyDesiredTelemetryIdentity(posthogInstance);
    }
    notifyTelemetryConsentListeners();
  }
}

function getResolvedTelemetryConfig(): Required<TelemetryConfig> | null {
  if (isTelemetryHardDisabled()) return null;

  return {
    apiKey: telemetryConfig.apiKey || DEFAULT_POSTHOG_API_KEY,
    apiHost: telemetryConfig.apiHost || DEFAULT_POSTHOG_HOST,
    uiHost: telemetryConfig.uiHost || DEFAULT_POSTHOG_UI_HOST,
  };
}

/**
 * Check if we're in a browser environment
 */
function isBrowser(): boolean {
  return typeof window !== "undefined" && typeof localStorage !== "undefined";
}

/**
 * Lazily load PostHog to avoid SSR/Node.js issues.
 * PostHog is a browser-only library, so we dynamically import it only when needed.
 */
async function getPostHog(): Promise<PostHog | null> {
  if (!isBrowser()) {
    return null;
  }

  if (posthogInstance) {
    return posthogInstance;
  }

  try {
    const { default: posthog } = await import("posthog-js");
    return posthog;
  } catch {
    // Failed to load PostHog - telemetry will be disabled
    return null;
  }
}

/**
 * Check if telemetry is disabled via environment variable or browser setting.
 * Works in both Node.js and browser (Vite) environments.
 */
function isDoNotTrackEnabled(): boolean {
  if (telemetryDisabled) {
    return true;
  }

  // Check Vite environment variable (browser)
  if (
    typeof import.meta !== "undefined" &&
    import.meta.env?.VITE_DO_NOT_TRACK === "1"
  ) {
    return true;
  }

  // Runtime-injected window global (see file header).
  if (isRuntimeDoNotTrackEnabled()) {
    return true;
  }

  // Check Node.js environment variable (SSR/testing)
  if (typeof process !== "undefined" && process.env?.DO_NOT_TRACK === "1") {
    return true;
  }

  // Check browser's navigator.doNotTrack standard
  if (
    typeof navigator !== "undefined" &&
    (navigator.doNotTrack === "1" ||
      // @ts-expect-error - Some browsers use window.doNotTrack
      (typeof window !== "undefined" && window.doNotTrack === "1"))
  ) {
    return true;
  }

  return false;
}

/**
 * Initialize PostHog SDK.
 *
 * @param enableCapturing - If true, enable capturing immediately (for install tracking).
 *                          If false, start with capturing disabled (for consent-gated tracking).
 */
export function configurePostHogBootstrap(
  bootstrap: BootstrapConfig | undefined,
): void {
  if (!posthogInstance) {
    pendingBootstrap = bootstrap;
  }
}

export async function initializePostHogClient(
  enableCapturing = false,
): Promise<PostHog | null> {
  if (posthogInstance) {
    return posthogInstance;
  }

  if (initializationPromise) {
    return initializationPromise;
  }

  initializationPromise = (async () => {
    const config = getResolvedTelemetryConfig();
    if (!config) {
      return null;
    }

    const posthog = await getPostHog();
    if (!posthog) {
      return null;
    }

    // A named instance isolates Canvas configuration, consent, identity, and
    // persistence from a host application's default PostHog singleton.
    const initializedPostHog = posthog.init(
      config.apiKey,
      {
        api_host: config.apiHost,
        ui_host: config.uiHost,
        opt_out_capturing_by_default: !enableCapturing,
        persistence: "localStorage",
        persistence_name: POSTHOG_INSTANCE_NAME,
        consent_persistence_name: `${POSTHOG_INSTANCE_NAME}-consent`,
        person_profiles: "always",
        capture_pageview: POSTHOG_PAGEVIEW_CAPTURE_MODE,
        autocapture: true,
        disable_session_recording: true,
        bootstrap: pendingBootstrap,
        before_send: addCanvasEventProperties,
      },
      POSTHOG_INSTANCE_NAME,
    );
    if (!initializedPostHog) return null;

    posthogInstance = initializedPostHog;
    pendingBootstrap = undefined;

    if (telemetryDisabled) {
      posthogInstance.opt_out_capturing();
    } else if (getTelemetryConsent() === "granted") {
      posthogInstance.opt_in_capturing();
      applyDesiredTelemetryIdentity(posthogInstance);
    } else if (!enableCapturing) {
      posthogInstance.opt_out_capturing();
    }

    return posthogInstance;
  })();

  try {
    return await initializationPromise;
  } finally {
    if (!posthogInstance) {
      initializationPromise = null;
    }
  }
}

/**
 * Get user's telemetry consent preference
 */
export function getTelemetryConsent(): TelemetryConsent {
  if (!isBrowser()) {
    return "pending";
  }

  // Check environment variable for opt-out
  if (isDoNotTrackEnabled()) {
    return "denied";
  }

  if (getLockedCloudAuthMode() === "cookie") {
    return "granted";
  }

  try {
    const consent = localStorage.getItem(TELEMETRY_CONSENT_KEY);
    if (consent === "granted" || consent === "denied") {
      return consent;
    }
  } catch {
    // Ignore storage errors
  }

  return "pending";
}

/**
 * Return an explicit browser choice that still needs to survive a Cloud login.
 * It remains pending across local backends so their settings cannot consume a
 * decision that must still be applied after the user connects to Cloud.
 */
export function getPendingCloudTelemetryConsent(): ResolvedTelemetryConsent | null {
  if (!isBrowser()) return null;

  try {
    const consent = localStorage.getItem(
      TELEMETRY_CONSENT_PENDING_CLOUD_SYNC_KEY,
    );
    return consent === "granted" || consent === "denied" ? consent : null;
  } catch {
    return null;
  }
}

/** Return the pre-reset actor whose local Automation consent must be revoked. */
export function getPendingLocalTelemetryRevocationId(): string | null {
  if (!isBrowser()) return null;

  try {
    return localStorage.getItem(TELEMETRY_CONSENT_PENDING_LOCAL_REVOCATION_KEY);
  } catch {
    return null;
  }
}

export function subscribeTelemetryConsent(listener: () => void): () => void {
  if (!isBrowser()) return () => {};
  const handleStorage = (event: StorageEvent) => {
    if (
      event.key === TELEMETRY_CONSENT_KEY ||
      event.key === TELEMETRY_CONSENT_PENDING_CLOUD_SYNC_KEY
    ) {
      listener();
    }
  };
  window.addEventListener(TELEMETRY_CONSENT_CHANGE_EVENT, listener);
  window.addEventListener("storage", handleStorage);

  return () => {
    window.removeEventListener(TELEMETRY_CONSENT_CHANGE_EVENT, listener);
    window.removeEventListener("storage", handleStorage);
  };
}

function notifyTelemetryConsentListeners(): void {
  if (isBrowser())
    window.dispatchEvent(new Event(TELEMETRY_CONSENT_CHANGE_EVENT));
}

function markTelemetryConsentForCloudSync(
  consent: ResolvedTelemetryConsent,
): void {
  if (!isBrowser()) return;

  try {
    localStorage.setItem(TELEMETRY_CONSENT_PENDING_CLOUD_SYNC_KEY, consent);
  } catch {
    // Ignore storage errors; the in-browser consent decision still applies.
  }
}

function markPendingLocalTelemetryRevocation(distinctId: string | null): void {
  if (!isBrowser() || !distinctId) return;

  try {
    localStorage.setItem(
      TELEMETRY_CONSENT_PENDING_LOCAL_REVOCATION_KEY,
      distinctId,
    );
  } catch {
    // Ignore storage errors; browser capture is still disabled below.
  }
}

export function clearPendingLocalTelemetryRevocation(
  expectedDistinctId: string,
): void {
  if (!isBrowser()) return;

  try {
    if (
      localStorage.getItem(TELEMETRY_CONSENT_PENDING_LOCAL_REVOCATION_KEY) !==
      expectedDistinctId
    ) {
      return;
    }
    localStorage.removeItem(TELEMETRY_CONSENT_PENDING_LOCAL_REVOCATION_KEY);
  } catch {
    // Ignore storage errors.
  }
}

export function clearPendingCloudTelemetryConsent(
  expected?: ResolvedTelemetryConsent,
): void {
  if (!isBrowser()) return;

  try {
    if (
      expected !== undefined &&
      localStorage.getItem(TELEMETRY_CONSENT_PENDING_CLOUD_SYNC_KEY) !==
        expected
    ) {
      return;
    }
    localStorage.removeItem(TELEMETRY_CONSENT_PENDING_CLOUD_SYNC_KEY);
    notifyTelemetryConsentListeners();
  } catch {
    // Ignore storage errors.
  }
}

/**
 * Set user's telemetry consent preference
 */
export async function setTelemetryConsent(
  consent: ResolvedTelemetryConsent,
  { syncToCloud = true }: SetTelemetryConsentOptions = {},
): Promise<void> {
  if (!isBrowser()) {
    return;
  }

  try {
    const previousConsent = getTelemetryConsent();
    const unchanged = previousConsent === consent;
    localStorage.setItem(TELEMETRY_CONSENT_KEY, consent);
    if (isTelemetryHardDisabled()) return;
    if (unchanged) return;

    // Reuse an initialized client synchronously so a same-flush identify()
    // cannot run before consent is applied. Only the cold path awaits import.
    const posthog = posthogInstance ?? (await initializePostHogClient());
    if (!posthog) {
      return;
    }

    if (consent === "granted") {
      posthog.opt_in_capturing();
      applyDesiredTelemetryIdentity(posthog);
    } else {
      markPendingLocalTelemetryRevocation(posthog.get_distinct_id?.() ?? null);
      if (posthog.get_property?.("$user_id") != null) {
        resetPostHogIdentity(posthog);
      } else {
        posthog.opt_out_capturing();
      }
    }
  } catch {
    // Ignore storage errors
  } finally {
    // Notify UI/backend/identity reconcilers only after the browser capture state
    // reflects this decision. Otherwise a pre-login grant can trigger an
    // identify while PostHog is still opted out and lose funnel continuity.
    if (syncToCloud) {
      markTelemetryConsentForCloudSync(consent);
    }
    notifyTelemetryConsentListeners();
  }
}

/**
 * Declare the current Cloud identity. The telemetry service applies it only
 * after consent and owns all reset/account-switch semantics.
 */
export async function setTelemetryIdentity(
  distinctId: string | null,
  properties: Record<string, unknown> = {},
): Promise<void> {
  const nextIdentity = distinctId === null ? null : { distinctId, properties };
  const unchanged =
    desiredTelemetryIdentity === nextIdentity ||
    (desiredTelemetryIdentity !== undefined &&
      desiredTelemetryIdentity !== null &&
      nextIdentity !== null &&
      desiredTelemetryIdentity.distinctId === nextIdentity.distinctId &&
      propertiesEqual(desiredTelemetryIdentity.properties, properties));
  if (unchanged) return;

  desiredTelemetryIdentity = nextIdentity;
  desiredIdentityRevision += 1;
  appliedIdentityRevision = -1;

  if (!isTelemetryEnabled()) return;
  const posthog = posthogInstance ?? (await initializePostHogClient());
  if (posthog && isTelemetryEnabled()) {
    // Read the desired identity after the await so a newer account always wins
    // if identity changes while the SDK is loading.
    applyDesiredTelemetryIdentity(posthog);
  }
}

/**
 * Check if telemetry is enabled (user has granted consent)
 */
export function isTelemetryEnabled(): boolean {
  return getTelemetryConsent() === "granted";
}

/**
 * Check if first use event has already been sent
 */
function hasFirstUseSent(): boolean {
  if (!isBrowser()) {
    return false;
  }

  try {
    return localStorage.getItem(TELEMETRY_FIRST_USE_KEY) === "true";
  } catch {
    return false;
  }
}

/**
 * Mark first use event as sent
 */
function markFirstUseSent(): void {
  if (!isBrowser()) {
    return;
  }

  try {
    localStorage.setItem(TELEMETRY_FIRST_USE_KEY, "true");
  } catch {
    // Ignore storage errors
  }
}

/**
 * Track the initial install of the library.
 *
 * IMPORTANT: This is sent immediately on first use, regardless of consent status.
 * This allows us to track library adoption even if users haven't made a consent choice yet.
 *
 * The event is:
 * - Completely anonymous (no PII, just a random PostHog distinct_id)
 * - Sent only once per installation (tracked via localStorage, persists across sessions)
 * - Still respects DO_NOT_TRACK environment variable and browser setting
 *
 * Users who want complete privacy can:
 * - Set VITE_DO_NOT_TRACK=1 or browser's Do Not Track
 * - Later deny consent to prevent all future tracking
 */
export async function trackInstall(): Promise<void> {
  // Respect hard opt-out via environment variable or browser setting
  if (isDoNotTrackEnabled()) {
    return;
  }

  // Already sent install event (persisted in localStorage - survives app relaunches)
  if (hasFirstUseSent()) {
    return;
  }

  // Initialize PostHog with capturing enabled (for this one event)
  const posthog = await initializePostHogClient(true);
  if (!posthog || isDoNotTrackEnabled()) {
    return;
  }

  // Temporarily enable capturing if it was disabled
  const wasOptedOut = posthog.has_opted_out_capturing?.() ?? false;
  if (wasOptedOut) {
    posthog.opt_in_capturing();
  }

  // Capture the install event
  posthog.capture("canvas_install", {
    platform: typeof navigator !== "undefined" ? navigator.platform : "unknown",
    user_agent:
      typeof navigator !== "undefined" ? navigator.userAgent : "unknown",
    referrer: typeof document !== "undefined" ? document.referrer : "",
    url_origin: typeof window !== "undefined" ? window.location.origin : "",
    embedded: typeof window !== "undefined" && window.self !== window.top,
  });

  // Mark as sent (stored in localStorage - persists across browser sessions)
  markFirstUseSent();

  // Restore opt-out state if user hasn't granted consent yet
  // This ensures we only send the install event, not subsequent events
  const currentConsent = getTelemetryConsent();
  if (currentConsent !== "granted") {
    posthog.opt_out_capturing();
  }
}

/**
 * Check if session start event has already been sent (this browser session)
 */
function hasSessionSent(): boolean {
  if (!isBrowser()) {
    return false;
  }

  try {
    return sessionStorage.getItem(TELEMETRY_SESSION_KEY) === "true";
  } catch {
    return false;
  }
}

/**
 * Mark session start event as sent (uses sessionStorage so it resets on new tabs/sessions)
 */
function markSessionSent(): void {
  if (!isBrowser()) {
    return;
  }

  try {
    sessionStorage.setItem(TELEMETRY_SESSION_KEY, "true");
  } catch {
    // Ignore storage errors
  }
}

/** Return the shared client only when a consented capture is safe to emit. */
async function getPostHogForConsentedCapture(): Promise<PostHog | null> {
  if (!isTelemetryEnabled() || getTelemetryConsent() !== "granted") return null;

  const posthog = await initializePostHogClient();
  if (!posthog || !isTelemetryEnabled()) return null;

  // The browser preference is the canonical capture decision. PostHog may
  // still carry an older opt-out marker while backend consent is loading or
  // after a previous backend temporarily reported a stale value. Heal that
  // drift at the event boundary so capture() cannot silently discard an event
  // that the user has explicitly allowed.
  if (posthog.has_opted_out_capturing?.()) {
    posthog.opt_in_capturing();
  }

  applyDesiredTelemetryIdentity(posthog);

  return posthog;
}

/**
 * Track a session start event.
 * Called each time a new browser session starts (respects consent).
 * Uses sessionStorage for deduplication - only sends once per browser session.
 */
export async function trackSessionStart(): Promise<void> {
  // Already sent session event this browser session
  if (hasSessionSent()) {
    return;
  }

  const posthog = await getPostHogForConsentedCapture();
  if (!posthog) return;

  posthog.capture("canvas_new_session", {
    is_first_use: !hasFirstUseSent(),
  });

  // Mark as sent for this session
  markSessionSent();
}

/** Return the active PostHog distinct ID when consent allows capture. */
export async function getTelemetryDistinctId(): Promise<string | null> {
  const posthog = await getPostHogForConsentedCapture();
  return posthog?.get_distinct_id?.() ?? null;
}

/** Return the PostHog distinct ID for local consent sync without emitting capture. */
export async function getTelemetryDistinctIdForConsentSync(): Promise<
  string | null
> {
  if (!isBrowser()) return null;

  if (getTelemetryConsent() !== "granted") {
    const pendingRevocationId = getPendingLocalTelemetryRevocationId();
    if (pendingRevocationId) return pendingRevocationId;
  }

  if (telemetryDisabled || isDoNotTrackEnabled()) return null;

  const posthog = await initializePostHogClient();
  return posthog?.get_distinct_id?.() ?? null;
}

/**
 * Track a custom event (respects consent).
 */
export async function trackEvent(
  eventName: string,
  properties: Record<string, unknown> = {},
): Promise<void> {
  const posthog = await getPostHogForConsentedCapture();
  if (!posthog) return;

  posthog.capture(eventName, properties);
}

/** Track an exception through the same consent-aware client as custom events. */
export async function trackException(
  error: unknown,
  properties: Record<string, unknown> = {},
): Promise<void> {
  const posthog = await getPostHogForConsentedCapture();
  if (!posthog) return;

  posthog.captureException(error, properties);
}

/**
 * Clear all telemetry data (for privacy/GDPR requests)
 */
export async function clearTelemetryData(): Promise<void> {
  if (!isBrowser()) {
    return;
  }

  try {
    markPendingLocalTelemetryRevocation(
      posthogInstance?.get_distinct_id?.() ?? null,
    );
    localStorage.removeItem(TELEMETRY_CONSENT_KEY);
    localStorage.removeItem(TELEMETRY_FIRST_USE_KEY);
  } catch {
    // Continue clearing the in-memory and SDK identity if storage is blocked.
  }
  clearPendingCloudTelemetryConsent();
  try {
    sessionStorage.removeItem(TELEMETRY_SESSION_KEY);
  } catch {
    // Continue clearing the in-memory and SDK identity if storage is blocked.
  }

  telemetryBackendContext = getBackendTelemetryProperties({});
  telemetryCloudContext = getCloudTelemetryProperties();
  desiredTelemetryIdentity = null;
  desiredIdentityRevision += 1;
  appliedIdentityRevision = -1;

  try {
    if (posthogInstance) {
      resetPostHogIdentity(posthogInstance, true);
    }
  } catch {
    // A reset failure must not leave capture enabled after a privacy clear.
    try {
      posthogInstance?.opt_out_capturing();
    } catch {
      // Telemetry failures must not break the application.
    }
  } finally {
    notifyTelemetryConsentListeners();
  }
}