import { Appearance, AppState, Dimensions, I18nManager, NativeModules, Platform, } from "react-native"; import type { AppInfo, DeviceContext } from "../types"; import { CONSTANTS } from "../constants"; const resolveLocale = (): string => { const settingsManager = NativeModules.SettingsManager; const localeFromIos = settingsManager?.settings?.AppleLocale ?? settingsManager?.settings?.AppleLanguages?.[0]; const localeFromAndroid = NativeModules.I18nManager?.localeIdentifier; return ( localeFromIos ?? localeFromAndroid ?? Intl.DateTimeFormat().resolvedOptions().locale ?? (I18nManager.isRTL ? "ar" : "en") ); }; const resolveTimezone = (): string => { return Intl.DateTimeFormat().resolvedOptions().timeZone ?? "UTC"; }; const resolveDeviceType = (width: number): DeviceContext["deviceType"] => { if (width >= 1024) { return "tablet"; } return "mobile"; }; export const getDefaultDeviceContext = (appInfo: AppInfo = {}): DeviceContext => { const window = Dimensions.get("window"); const colorScheme = Appearance.getColorScheme(); return { sdkVersion: CONSTANTS.sdkVersion, platform: Platform.OS, platformVersion: String(Platform.Version), deviceType: resolveDeviceType(window.width), locale: resolveLocale(), timezone: resolveTimezone(), appState: AppState.currentState, colorScheme: colorScheme === "unspecified" ? null : colorScheme, screen: { width: window.width, height: window.height, scale: window.scale, fontScale: window.fontScale, }, app: appInfo, }; };