<script lang="ts">
  import Config from './pages/Config.svelte'
  import Desktop from '$lib/components/desktop-shell/Desktop.svelte'
  import { subscribeContext } from '$lib/stores/context'
  import { openAppWindow, openEntityWindow } from '$lib/stores/windows'
  import { isConfigured } from '$lib/config'
  import { onMount } from 'svelte'
  import { processPendingCallback, initOIDC } from '$lib/oidc'
  import { Toaster } from '$lib/components/ui/sonner'

  let configured = $state(isConfigured())

  // Old hash routes (#/kb, #/entity/<slug>, ...) from the sidebar-shell era —
  // translated into opening the equivalent window once, then cleared, so
  // links/bookmarks from before the desktop redesign keep working without
  // reintroducing a router.
  const LEGACY_APP_ROUTES: Record<string, string> = {
    overview: 'tasks',
    chat: 'tasks',
    kb: 'kb',
    entities: 'kb',
    graph: 'kb',
    ops: 'ops',
    signals: 'signals',
    knowledge: 'knowledge',
    learning: 'learning'
  }

  function resolveLegacyHash() {
    const path = location.hash.slice(2)
    if (!path) return
    const [head, ...rest] = path.split('/')
    if (head === 'entity' && rest.length) {
      openEntityWindow(rest.join('/'))
    } else if (LEGACY_APP_ROUTES[head]) {
      openAppWindow(LEGACY_APP_ROUTES[head])
    }
    history.replaceState(null, '', location.pathname + location.search)
  }

  onMount(async () => {
    if (await processPendingCallback()) {
      configured = true
    } else if (!configured) {
      if (await initOIDC()) configured = true
    }
    resolveLegacyHash()
  })

  // Context (dashboard summary + approvals poll) and the SSE stream both
  // authenticate — don't subscribe until a token exists.
  $effect(() => {
    if (!configured) return
    return subscribeContext()
  })
</script>

{#if !configured}
  <Config
    onConnected={() => (configured = true)}
    onCancel={isConfigured() ? () => (configured = true) : undefined}
  />
{:else}
  <Toaster />
  <Desktop />
{/if}
