import { useEffect, useState } from 'react'
import { GetDeviceInfo } from '@/wailsjs/go/main/App'
import type { device } from '@/wailsjs/go/models'

export type DeviceInfo = device.DeviceInfo

export function useDeviceInfo(deviceId: string | null) {
  const [info, setInfo] = useState<DeviceInfo | null>(null)
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState<string | null>(null)

  useEffect(() => {
    if (!deviceId) {
      setInfo(null)
      return
    }
    setLoading(true)
    setError(null)
    GetDeviceInfo(deviceId)
      .then(setInfo)
      .catch((e) => setError(String(e)))
      .finally(() => setLoading(false))
  }, [deviceId])

  return { info, loading, error }
}
