import { LOCALE_STORAGE_KEY, type SupportedLocale } from '@/locales'

/** 前端 locale → 后端 lang 请求头 / CMS 参数 */
export function toBackendLang(locale: string): string {
  switch (locale as SupportedLocale) {
    case 'zh-CN':
      return 'zh_CN'
    case 'zh-TW':
      return 'zh_HK'
    case 'en':
      return 'en_US'
    case 'ko':
      return 'ko_KR'
    case 'ja':
      return 'ja_JP'
    case 'hi':
      return 'hi_IN'
    case 'id':
      return 'id_ID'
    default:
      return 'zh_CN'
  }
}

/** axios 拦截器用：读取用户当前语言 */
export function getRequestLang(): string {
  try {
    const stored = localStorage.getItem(LOCALE_STORAGE_KEY)
    if (stored) return toBackendLang(stored)
  } catch {
    // ignore
  }
  return toBackendLang('zh-CN')
}

/** CMS / 协议类接口 lang 参数（与 MyLeadView 等保持一致） */
export const cmsLang = toBackendLang
