import { useEffect } from 'react'
import { useWaste } from './store'
import { Onboarding } from './pages/Onboarding'
import { Chat } from './pages/Chat'
import './App.css'

const isLocal = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
const DAEMON_WS = import.meta.env.VITE_DAEMON_WS ?? 'ws://127.0.0.1:17338'
const cfg = (window as unknown as { WASTE_CONFIG?: { signalURL?: string } }).WASTE_CONFIG
// Use browser mode if a signal URL is configured, even on localhost
const useBrowser = !isLocal || !!cfg?.signalURL

export default function App() {
  const { connect, connectBrowser, daemonStatus, localPeer } = useWaste()

  useEffect(() => {
    if (useBrowser) {
      connectBrowser()
    } else {
      connect(DAEMON_WS)
    }
  }, [connect, connectBrowser])

  if (daemonStatus !== 'connected' || !localPeer) {
    return <Onboarding status={daemonStatus} />
  }

  return <Chat />
}
