<template>
  <div id="app">
    <el-config-provider :locale="getLang">
      <router-view></router-view>
    </el-config-provider>
  </div>
</template>

<script setup>
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import en from 'element-plus/es/locale/lang/en'
import vi from 'element-plus/es/locale/lang/vi'
import { useI18n } from 'vue-i18n'
import socket from '@/utils/websocket.js'
import {computed} from "vue";
socket.connect();


const { locale } = useI18n()

const getLang = computed(() => {
  if(locale.value === 'zh') return zhCn
  else if(locale.value === 'en') return en
  else if(locale.value === 'vi') return vi
})

onMounted(() => {
  // 1. 安全地获取并判断 window.global.openAPPURL 是否存在
  const openAppUrl = window?.global?.openAPPURL

  if (openAppUrl) {
    // 2. 判断当前窗口大小是否为 App/移动端 大小
    // 这里以常见的 768px 作为移动端宽度的分界线，你可以根据实际项目的 UI 规范调整该值
    const isAppSize = window.innerWidth <= 768

    // 3. 满足条件则跳转
    if (isAppSize) {
      let finalUrl = openAppUrl
      
      // 解析当前 URL 参数
      const urlParams = new URLSearchParams(window.location.search)
      const token = urlParams.get('token')
      const isBind = urlParams.get('is_bind')

      // 判断是否有 token 并拼接
      if (token) {
        finalUrl += finalUrl.includes('?') ? `&token=${token}` : `?token=${token}`
        // 如果同时存在 is_bind，继续拼接
        if (isBind) {
          finalUrl += `&is_bind=${isBind}`
        }
      }

      window.location.href = finalUrl
    }
  }
})
</script>

<style scoped>
</style>