<template>
  <view class="cwg-language" >
    <cwg-dropdown :menu-list="customMenuList" @menuClick="handleMenuClick" showActive :activeKey="lang">
      <view class="pc-header-btn cursor-pointer" :data-tooltip="t('vu.tooltip.t101')">
        <cwg-icon name="cwg-lang" :color="iconColor " :size="20" />
        <text class="lang-text">{{ t(`language.${lang}`) }}</text>
      </view>
    </cwg-dropdown>
  </view>

</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { localesList, switchAppLanguage } from '@/locale/index'
import { lang } from '@/composables/config'

const props = defineProps({
  iconColor: {
    type: String,
    default: '#97A1C0'
  },
})

const { t, locale } = useI18n()
const customMenuList = computed(() =>
  localesList.map((code) => ({
    label: t(`language.${code}`),
    type: code
  })),
)

function handleMenuClick({ value }: { value: any }) {
  const type = value?.type ?? value
  switchAppLanguage(type, { locale, lang })
}

function getFlagSrc(code: string) {
  switch (code) {
    case 'en':
      return '/static/flag-en.png'
    case 'cn':
      return '/static/flag-cn.png'
    case 'zhHant':
      return '/static/flag-zhHant.png'
    case 'es':
      return '/static/flag-es.png'
    default:
      return '/static/flag-en.png'
  }
}
</script>

<style scoped lang="scss">
@import "@/uni.scss";

.pc-header-btn {
  width: auto;
  display: flex;
  align-items: center;
  cursor: pointer;
  gap: px2rpx(6);
  padding: 0 px2rpx(5);
}
.cwg-language {
  :deep(.cwg-dropdown-menu-container) {
    left: px2rpx(-40) !important;
    right: px2rpx(0) !important;
  }
  @media screen and (max-width: 991px) {
    :deep(.cwg-dropdown-menu-container) {
      right: px2rpx(-20) !important;
      .menu{
        max-height: 70vh;
        overflow-y: scroll;
        scrollbar-width: thin;
      }
    }
  }
  @media screen and (max-width: 768px) {
    :deep(.cwg-dropdown-menu-container) {
      right: px2rpx(0) !important;
    }
  }
}


:deep(.cwg-dropdown-menu-container .menu .menu-item) {
  min-height: px2rpx(36);
}

:deep(.cwg-dropdown) {
  overflow: visible !important;
}
</style>
