// Shared launch-diagnostic plumbing for the dedicated Mini App entries (/telegram/, /vk/). When one of
// those is opened without a valid launch — a plain browser tab, or a client that delivered no sign-in
// data — the app renders a compact, privacy-safe diagnostic snapshot (the LaunchError screen) the user
// can screenshot or share, instead of silently proceeding (to the marketing landing, or as a guest).
// This module holds the pieces both platforms share: the neutral report shape the screen renders, the
// client environment lines, and a query-string field-NAME reader (names only — the values can be auth
// material). The platform-specific collectors live in telegram.ts / vk.ts.

/**
 * LaunchDiag is the neutral, pre-formatted snapshot the LaunchError screen renders, so the screen is
 * platform-agnostic: it shows the report text and, only when retry is set, a Retry button. The
 * collectors (telegramLaunchDiag / vkLaunchDiag) own the platform-specific formatting.
 */
export interface LaunchDiag {
  /** platform selects the screen's title copy ('telegram' | 'vk'). */
  platform: 'telegram' | 'vk';
  /** report is the compact multi-line "key: value" snapshot, sized to fit one screenshot. */
  report: string;
  /** retry is whether the screen shows a Retry button — true only where a late-arriving launch can
   *  still succeed (Telegram initData), false where the launch is fixed at load (VK's signed URL). */
  retry: boolean;
}

interface uaBrand {
  brand: string;
  version: string;
}

interface uaDataValue {
  platform?: string;
  mobile?: boolean;
  brands?: uaBrand[];
}

/** uaData returns the User-Agent Client Hints object (Chromium only — notably the Android in-app
 *  webviews), or undefined where it is unavailable (iOS / Safari / Firefox). */
export function uaData(): uaDataValue | undefined {
  if (typeof navigator === 'undefined') return undefined;
  return (navigator as unknown as { userAgentData?: uaDataValue }).userAgentData;
}

/**
 * clientEnvLines returns the shared client-environment lines of a launch diagnostic — the OS / mobile
 * flag, the browser brands, and the full User-Agent — read from Client Hints (with a navigator.platform
 * fallback). Both the Telegram and VK reports append these, so the environment readout stays identical
 * across platforms. No secrets: it carries only client identification, never an IP.
 */
export function clientEnvLines(): string[] {
  const ua = uaData();
  const navPlatform =
    typeof navigator === 'undefined' ? '' : ((navigator as unknown as { platform?: string }).platform ?? '');
  const osPlatform = ua?.platform ?? navPlatform;
  const mobile = ua?.mobile === undefined ? '' : ua.mobile ? 'yes' : 'no';
  const browser = (ua?.brands ?? []).map((b) => `${b.brand} ${b.version}`).join(', ');
  const userAgent = typeof navigator === 'undefined' ? '' : navigator.userAgent;
  return [`os: ${osPlatform || '—'}   mobile: ${mobile || '—'}`, `browser: ${browser || '—'}`, `ua: ${userAgent || '—'}`];
}

/**
 * queryFieldNames parses a URL query string (or any `key=value&…` form) and returns only its field
 * NAMES, never the values — the values (a Telegram initData hash, a VK sign) are auth material that must
 * never surface in a shareable diagnostic. Returns an empty array for empty or unparseable input.
 */
export function queryFieldNames(raw: string): string[] {
  if (!raw) return [];
  try {
    return [...new URLSearchParams(raw).keys()];
  } catch {
    return [];
  }
}
