export type RuntimePlatform = 'electron' | 'capacitor' | 'browser';

export interface PlatformDetectionInput {
  hasElectronApi: boolean;
  capacitorIsNative?: boolean;
}

type CapacitorWindow = Window & {
  Capacitor?: {
    isNativePlatform?: () => boolean;
  };
};

/** Resolve the active runtime shell used by the product client. */
export function detectRuntimePlatform(input: PlatformDetectionInput): RuntimePlatform {
  if (input.hasElectronApi) {
    return 'electron';
  }

  if (input.capacitorIsNative) {
    return 'capacitor';
  }

  return 'browser';
}

/** Best-effort detection of a Capacitor native shell without importing Capacitor modules. */
export function isCapacitorNativeRuntime(): boolean {
  if (typeof window === 'undefined') {
    return false;
  }

  const capacitor = (window as CapacitorWindow).Capacitor;

  return capacitor?.isNativePlatform?.() === true;
}

/** Whether the chat composer should expose a tap-to-attach control instead of drag-and-drop only. */
export function shouldUseMobileAttachmentPicker(runtime: RuntimePlatform, isMobileViewport: boolean): boolean {
  if (runtime === 'electron') {
    return false;
  }

  return runtime === 'capacitor' || isMobileViewport;
}
