import { mount } from 'svelte'
import App from './App.svelte'
import './app.css'
import { initConfig, setConfig, getConfig } from '$lib/config'

function handleDesktopToken() {
  const params = new URLSearchParams(location.search)
  const token = params.get('token')
  if (token && new URLSearchParams(location.search).has('desktop')) {
    const apiUrl = params.get('apiUrl') || getConfig().apiUrl || ''
    setConfig({ apiUrl, token, isDesktop: true })
    initConfig({ apiUrl, token, isDesktop: true })
    params.delete('token')
    const q = params.toString()
    history.replaceState(null, '', location.pathname + (q ? '?' + q : ''))
    return true
  }
  return false
}

function start() {
  initConfig()
  handleDesktopToken()

  requestAnimationFrame(() => import('./lib/renderers'))

  mount(App, { target: document.getElementById('app')! })
}

start()
