// Detects whether the Tauri runtime is present in the current window.
//
// In Tauri v2 the bootstrap script injects `window.__TAURI_INTERNALS__`
// before the page script runs, and also sets `window.isTauri = true` as
// a convenience marker. Either is sufficient evidence we are inside a
// Tauri webview.
//
// In a plain browser (vite preview, `npm run dev` outside `tauri dev`)
// neither global is set, so `hasTauriRuntime()` returns false. Callers
// use this to gate `invoke()` calls and fall back to localStorage-only
// behaviour for browser-preview mode.

export function hasTauriRuntime() {
  if (typeof window === "undefined") return false;
  if (window.__TAURI_INTERNALS__) return true;
  if (window.isTauri === true) return true;
  return false;
}

// Coarse-grained mobile / Android detection. Used by the Svelte UI to
// hide features that depend on desktop-only Tauri commands (multi-window,
// global hotkeys, auto-paste, meeting auto-capture, system tray) so an
// Android build's frontend renders only the surfaces that actually work.
//
// `isMobile()` is the broader check (covers iOS and Android) and falls
// back on the user-agent for non-Tauri previews. `isAndroid()` is the
// targeted check used where the platform genuinely matters (e.g.
// foreground-notification record button vs global hotkey).
//
// Tauri 2 doesn't expose a synchronous platform-detection helper that
// works during the initial Svelte render, so we rely on user-agent
// sniffing — fine for feature-gating UI, never for security decisions.

const ANDROID_UA_RE = /android/i;
const MOBILE_UA_RE = /android|iphone|ipad|ipod|mobile/i;

export function isAndroid(): boolean {
  if (typeof navigator === "undefined") return false;
  return ANDROID_UA_RE.test(navigator.userAgent);
}

export function isMobile(): boolean {
  if (typeof navigator === "undefined") return false;
  return MOBILE_UA_RE.test(navigator.userAgent);
}
