import { api } from './config.js';

// 获取当前语言
const getCurrentLanguage = () => {
  if (process.client) {
    try {
      // 从 cookie 获取语言设置（Nuxt i18n 自动管理）
      const cookieLanguage = document.cookie
        .split('; ')
        .find(row => row.startsWith('i18n_redirected='))
        ?.split('=')[1];
      
      if (cookieLanguage && ['cn', 'en'].includes(cookieLanguage)) {
        return cookieLanguage;
      }
      
      // 如果没有 cookie，检测浏览器语言
      const browserLang = navigator.language?.toLowerCase() || 'en';
      if (browserLang.startsWith('zh')) {
        return 'cn';
      }
      return 'en';
    } catch (error) {
      console.warn('API 服务语言检测失败:', error);
      return 'en';
    }
  }
  return 'en'; // 服务端默认英文
};

// 首页轮播图
export const GetCarouseApi = () => {
  return api("/website/get/homePageCarousel_list", {
    method: 'POST',
    body: {
      locale: getCurrentLanguage()
    }
  });
};

// 产品分类
export const GetProductCategoryApi = () => {
  return api("/website/get/productCategory_list", {
    method: 'POST',
    body: {
      locale: getCurrentLanguage()
    }
  });
};

// 产品列表
export const GetProductApi = (data) => {
  return api("/website/get/product_list", {
    method: 'POST',
    body: data
  });
};

// 产品详情
export const GetProductDetailApi = (data) => {
  return api("/website/get/product_detail", {
    method: 'POST',
    body: data
  });
};

// 资讯列表
export const GetNewsApi = (data) => {
  return api("/website/get/news_list", {
    method: 'POST',
    body: data
  });
};

// 资讯详情
export const GetNewsDetailApi = (data) => {
  return api("/website/get/news_detail", {
    method: 'POST',
    body: data
  });
};

// 联系我们提交
export const GetMessageApi = (data) => {
  return api("/website/add/message", {
    method: 'POST',
    body: data
  });
};

// 证书列表
export const GetCertificateApi = () => {
  return api("/website/get/certificate_list", {
    method: 'POST',
    body: {
      locale: getCurrentLanguage()
    }
  });
};

// app下载
export const GetDownloadApi = () => {
  return api("/website/get/appInstallPackage", {
    method: 'POST',
    body: {
      locale: getCurrentLanguage()
    }
  });
};