<script setup lang="ts">
import { zhCN, dateZhCN, zhTW, dateZhTW, enUS, dateEnUS } from 'naive-ui'
import { provide, ref } from 'vue'
import mitt from 'mitt'
import { themeOverrides } from '@/config/theme-config'
import { useResizeObserver } from '@vueuse/core'
import { useDesignSettingStore } from '@/store/modules/design-setting'
import { useUserStore } from './store/modules/user'
import MessageTipModal from '@/components/message-tip-modal/message-tip-modal.vue'
import { useSystemLanguageStore } from '@/store/modules/system-language'

const designSettingStore = useDesignSettingStore()
const userStore = useUserStore()
const systemLanguageStore = useSystemLanguageStore()

const emitter = mitt<MittEvents>()

provide('emitter', emitter)

// const currentLocale = ref(zhCN)
const currentLocale = ref(localeParser())
// const currentDateLocale = ref(dateZhCN)
const currentDateLocale = ref(dateLocaleParser())

const rootContainer = ref<HTMLDivElement | null>(null)

useResizeObserver(rootContainer, (entries) => {
  const entry = entries[0]
  const { width } = entry.contentRect

  /**
   * 0 < width <= 760 隐藏侧边栏
   * 760 < width <= 990 折叠侧边栏
   * width > 990 展开侧边栏
   */
  if (width <= 760) {
    designSettingStore.toggleIsMobileLayout(true)
    designSettingStore.toggleSidebarDisplayStatus('hidden')
  } else if (760 < width && width <= 990) {
    designSettingStore.toggleIsMobileLayout(false)
    designSettingStore.toggleSidebarDisplayStatus('collapse')
  } else {
    designSettingStore.toggleIsMobileLayout(false)
    designSettingStore.toggleSidebarDisplayStatus('expand')
  }
})

/* created */
;(function () {
  if (userStore.isLogin) {
    userStore.fetchUpdateUserInfo()
  }
})()

function localeParser() {
  switch (systemLanguageStore.currentLanguageInfo.key) {
    case 'zh-CN':
      return zhCN
    case 'zh-HK':
      return zhTW
    case 'en':
      return enUS
  }
}

function dateLocaleParser() {
  switch (systemLanguageStore.currentLanguageInfo.key) {
    case 'zh-CN':
      return dateZhCN
    case 'zh-HK':
      return dateZhTW
    case 'en':
      return dateEnUS
  }
}
</script>

<template>
  <div ref="rootContainer" class="h-full w-full">
    <MessageTipModal />

    <NConfigProvider
      class="h-full w-full"
      :locale="currentLocale"
      :date-locale="currentDateLocale"
      :theme-overrides="themeOverrides"
    >
      <RouterView v-slot="{ Component }">
        <Component :is="Component" />
      </RouterView>
    </NConfigProvider>
  </div>
</template>
