import defaultSettings from '@/settings'
import { useStorage } from '@vueuse/core'
import { useCookies } from '@vueuse/integrations/useCookies'
// 导入 Element Plus 中英文语言包
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import en from 'element-plus/es/locale/lang/en'
const cookies = useCookies()

// setup
export const useAppStore = defineStore('app', () => {
  // state
  const device = useStorage('device', 'desktop')
  const size = useStorage('size', defaultSettings.size)
  let lang = cookies.get('locale')
  if(!lang) lang = defaultSettings.language
  const language = useStorage('language', lang)
  const sidebarStatus = useStorage('sidebarStatus', 'closed')

  const sidebar = reactive({
    opened: sidebarStatus.value !== 'closed',
    withoutAnimation: false
  })
  const activeTopMenuPath = useStorage('activeTopMenuPath', '')
  /**
   * 根据语言标识读取对应的语言包
   */
  const locale = computed(() => {
    if (language?.value == 'en-us') {
      return en
    } else {
      return zhCn
    }
  })

  // actions
  function toggleSidebar() {
    sidebar.opened = !sidebar.opened
    if (sidebar.opened) {
      sidebarStatus.value = 'opened'
    } else {
      sidebarStatus.value = 'closed'
    }
  }

  function closeSideBar() {
    sidebar.opened = false
    sidebarStatus.value = 'closed'
  }

  function openSideBar() {
    sidebar.opened = true
    sidebarStatus.value = 'opened'
  }

  function toggleDevice(val) {
    device.value = val
  }

  function changeSize(val) {
    size.value = val
  }
  /**
   * 切换语言
   *
   * @param val
   */
  function changeLanguage(val) {
    language.value = val
    // cookie切换
    cookies.set('locale', val, { path: '/' })
  }
  /**
   * 混合模式顶部切换
   */
  function activeTopMenu(val) {
    activeTopMenuPath.value = val
  }

  return {
    device,
    sidebar,
    language,
    locale,
    size,
    activeTopMenu,
    toggleDevice,
    changeSize,
    changeLanguage,
    toggleSidebar,
    closeSideBar,
    openSideBar,
    activeTopMenuPath
  }
})
