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

// When served from the anchor (non-localhost), default to browser mode.
// When running locally, try the daemon first.
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'

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

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

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

  return <Chat />
}
