<script setup lang="ts">
import { NConfigProvider, NGlobalStyle } from 'naive-ui'
import { NaiveProvider } from '@/components/common'
import { useTheme } from '@/hooks/useTheme'
import { useLanguage } from '@/hooks/useLanguage'
import { emitter } from '@/plugins'
import { onMounted, ref, provide, watch, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import { useCompanyStore } from '@/store'
import { link } from 'fs'

const { theme, themeOverrides } = useTheme()
const { language } = useLanguage()

const { isMobile } = useBasicLayout()
const router = useRouter()
emitter.on('changeVipShow', res => {
    console.log(res)
    if (isMobile.value) {
        router.push('/vip')
    } else {
        emitter.emit('vipShowChat', !!res)
    }
})
function login() {
    if (isMobile.value) {
        router.replace({ name: 'login' })
    } else {
        router.replace({ name: 'home', query: { login: 'true' } })
    }
}
provide('login', login)
const companyStore = useCompanyStore()
const company = computed(() => {
    return companyStore.company
})
watch(
    company,
    () => {
        if (company.value.assistantName) {
            document.title = company.value.assistantName
        }
        if (company.value.logo) {
            let link = document.createElement('link')
            link.rel = 'icon'
            link.href = company.value.logo
            document.head.appendChild(link)
        } else {
            let link = document.createElement('link')
            link.rel = 'icon'
            link.href = 'https://nebuai.oss-cn-hangzhou.aliyuncs.com/image/logo/favicon.ico'
            document.head.appendChild(link)
        }
    },
    {
        deep: true,
        immediate: true
    }
)
</script>

<template>
    <NConfigProvider
        class="h-full"
        :theme="theme"
        :theme-overrides="themeOverrides"
        :locale="language"
        inline-theme-disabled
    >
        <NGlobalStyle />
        <NaiveProvider>
            <RouterView />
        </NaiveProvider>
    </NConfigProvider>
</template>
