"use client";

import { createTelemetryClient } from "@bytelyst/telemetry-client";
import { PLATFORM_SERVICE_URL, PRODUCT_ID, TELEMETRY_TRANSPORT } from "@/lib/product-config";

let initialized = false;

const telemetryClient = createTelemetryClient({
  productId: PRODUCT_ID,
  baseUrl: PLATFORM_SERVICE_URL,
  endpoint: "/telemetry/events",
  platform: "web",
  channel: "notes_web",
  transport: TELEMETRY_TRANSPORT,
  appVersion: "0.1.0",
  buildNumber: "1",
  releaseChannel: "dev",
  osFamily: "other",
});

export function initTelemetry() {
  if (initialized) return telemetryClient;
  telemetryClient.init();
  telemetryClient.trackEvent("info", "app_shell", "web_app_initialized");
  initialized = true;
  return telemetryClient;
}

export function trackPageView(page: string) {
  telemetryClient.trackEvent("info", "navigation", "page_view", {
    feature: page,
  });
}

export { telemetryClient };
