export default defineNuxtRouteMiddleware((to, from) => {
  if (import.meta.server) return

  const authStore = useAuthStore()
  const { token } = storeToRefs(authStore)
  const chatStore = useChatStore()
  const { user } = storeToRefs(chatStore)

  // 建立链接
  async function getUserInfo() {
    const { data } = await request('/website/tourism/user/view')
    chatStore.user = data
    user.value = data
    console.log(data, 'createConnection')

    await chatStore.createConnection(data.pass)

    // chatStore.reqChatList()

    // console.log('用户信息：', chatStore.user)

    // console.log('会话列表：', chatStore.chatList.value)
  }

  if (token.value) {
    getUserInfo()
    return
  }

  if (to.fullPath.includes('/profile')) {
    return navigateTo('/login', {
      replace: true,
      query: {
        redirect: to.fullPath
      }
    })
  }

  if (to.fullPath.includes('/note-create')) {
    return navigateTo('/login', {
      replace: true,
      query: {
        redirect: to.fullPath
      }
    })
  }
  return
})
