import type { Platform } from '~/types'

export function usePlatformDetection() {
  const platform = useState<Platform>('platform', () => 'unknown')

  function detectPlatform(): Platform {
    if (import.meta.server)
      return 'unknown'

    const ua = navigator.userAgent.toLowerCase()

    if (/iphone|ipad|ipod/.test(ua))
      return 'ios'
    else if (/android/.test(ua))
      return 'android'
    else if (/windows|macintosh|linux/.test(ua))
      return 'desktop'

    return 'unknown'
  }

  onMounted(() => {
    platform.value = detectPlatform()
  })

  return {
    platform: readonly(platform),
    isIOS: computed(() => platform.value === 'ios'),
    isAndroid: computed(() => platform.value === 'android'),
    isDesktop: computed(() => platform.value === 'desktop'),
  }
}
