<script lang="ts" setup>
import { useLocaleStore } from '@/store/modules/locale'
import { useLocale } from '@/hooks/web/useLocale'
import { propTypes } from '@/utils/propTypes'
import { useDesign } from '@/hooks/web/useDesign'

defineOptions({ name: 'LocaleSelect' }) // 修改组件名称

const { getPrefixCls } = useDesign()

const prefixCls = getPrefixCls('locale-select') // 修改类名前缀

defineProps({
  color: propTypes.string.def('')
})

const localeStore = useLocaleStore()

const langMap = computed(() => localeStore.getLocaleMap)

const currentLang = computed({
  get: () => localeStore.getCurrentLocale.lang,
  set: (lang: LocaleType) => {
    if (lang === unref(currentLang)) return
    // 需要重新加载页面让整个语言多初始化
    window.location.reload()
    localeStore.setCurrentLocale({
      lang
    })
    const { changeLocale } = useLocale()
    changeLocale(lang)
  }
})
</script>

<template>
  <ElSelect
    :class="prefixCls"
    v-model="currentLang"
  >
    <ElOption
      v-for="item in langMap"
      :key="item.lang"
      :label="item.name"
      :value="item.lang"
    />
  </ElSelect>
</template>
