import { reflectionApi } from './reflection'

export type VehicleMaintenanceMode = 'online' | 'offline' | 'repair' | 'blown'

const METHOD_MAP: Record<VehicleMaintenanceMode, string> = {
  online: 'OnlineCar',
  offline: 'OfflineCar',
  repair: 'Repair',
  blown: 'Blown'
}

export async function setVehicleMaintenance(
  carId: number,
  mode: VehicleMaintenanceMode
): Promise<boolean> {
  const method = METHOD_MAP[mode]
  if (!method) return false
  try {
    await reflectionApi.execute('car', carId, method)
    return true
  } catch {
    return false
  }
}

export function openOnboardWeb(url?: string | null, ip?: string | null): void {
  const target = url ?? (ip ? `http://${ip}:8081` : null)
  if (!target) return
  window.open(target, '_blank', 'noopener,noreferrer')
}
