/** Device / platform metadata attached to checkout analytics events. */ export type DeviceType = 'mobile' | 'tablet' | 'desktop'; export type CheckoutPlatform = 'web' | 'ios' | 'android'; /** Detect coarse device class from viewport / user agent (web only). */ export function detectDeviceType(userAgent?: string): DeviceType { if (typeof window !== 'undefined') { const w = window.innerWidth; if (w < 768) return 'mobile'; if (w < 1024) return 'tablet'; return 'desktop'; } const ua = (userAgent ?? '').toLowerCase(); if (/ipad|tablet/.test(ua)) return 'tablet'; if (/mobile|iphone|android/.test(ua)) return 'mobile'; return 'desktop'; } /** Web checkout platform; native widgets pass platform explicitly. */ export function detectPlatform(): CheckoutPlatform { return 'web'; } /** Best-effort user agent string (empty on native when unavailable). */ export function getUserAgent(): string | undefined { if (typeof navigator !== 'undefined' && navigator.userAgent) { return navigator.userAgent.slice(0, 512); } return undefined; } export interface DeviceContext { deviceType: DeviceType; platform: CheckoutPlatform; userAgent?: string; } /** Build device context for web embeds. */ export function getWebDeviceContext(): DeviceContext { const userAgent = getUserAgent(); return { deviceType: detectDeviceType(userAgent), platform: detectPlatform(), userAgent, }; }