export default defineNuxtPlugin(() => {
  const nuxtApp = useNuxtApp()
  const logtoUser = nuxtApp.ssrContext?.event.context.logtoUser ?? null
  const orgId = (logtoUser as { organizations?: string[] } | null)?.organizations?.[0] ?? null

  const userState = useState('logto-user', () => null)
  const orgState = useState<string | null>('logto-org-id', () => null)

  if (logtoUser) {
    userState.value = logtoUser
  }
  if (orgId) {
    orgState.value = orgId
  }
})
