import dotenv from 'dotenv'
import process from 'node:process'
import { currentLocales } from './i18n/i18n'
import fs from 'fs'
import path from 'path'
const envFile = process.env.ENV_FILE || '.env.test'
dotenv.config({ path: `./env/${envFile}` })
const publicConfig = Object.entries(process.env)
    .filter(([key]) => key.startsWith('NUXT_PUBLIC_'))
    .reduce((config, [key, value]) => {
      config[key] = value
      return config
    }, {})

let httpsOptions = {}

try {
  // 读取文件并转换为字符串
  const key = fs.readFileSync(path.resolve(__dirname, 'ssl/localhost-key.pem'), 'utf-8')
  const cert = fs.readFileSync(path.resolve(__dirname, 'ssl/localhost.pem'), 'utf-8')
  
  httpsOptions = { key, cert }
  } catch (error) {
  // 失败时使用HTTP
  httpsOptions = false
}

export default defineNuxtConfig({
  modules: [
    '@vant/nuxt',
    '@unocss/nuxt',
    '@nuxtjs/i18n'
  ],
  runtimeConfig: {
    // 私有配置,只有在服务端可用
    apiSecret: process.env.NUXT_API_SECRET,
    // 公共配置,客户端和服务端都可用
    public: publicConfig,
  },
  css: [
    '@unocss/reset/tailwind.css',
    '@/static/styles/default-theme.css',
  ],

  postcss: {
    plugins: {
      'autoprefixer': {},
      'postcss-px-to-viewport': {
        viewportWidth: 375, // 设计稿宽度
        viewportUnit: 'vmin', // 关键配置
        fontViewportUnit: 'vmin', // 字体单位
        unitPrecision: 5,
        propList: ['*'],
        selectorBlackList: [],
        minPixelValue: 1,
        mediaQuery: false,
        exclude: /@nuxt/
      }
    },
  },
  i18n: {
    locales: currentLocales,
    lazy: true,
    strategy: 'no_prefix',
    detectBrowserLanguage: {
      useCookie: false,
      cookieKey: 'i18n_redirected',
      redirectOn: 'root',
      alwaysRedirect: true,
    },
    defaultLocale: 'zh-CN',
    vueI18n: './i18n/i18n.config.ts',
  },

  app: {
    layoutTransition: {
      name: 'layout',
      mode: 'out-in'
    },
    head: {
      viewport: 'width=device-width,initial-scale=1,viewport-fit=cover',
      link: [
        { rel: 'icon', href: '/favicon.ico', sizes: 'any' },
      ],
      // stripe支付CDN引用
      script: [
        // {
        //   src: 'https://js.stripe.com/v3/',
        //   defer: true // 可选,建议添加 defer
        // }
      ],
      meta: [
        { name: 'viewport', content: 'width=device-width, initial-scale=1, viewport-fit=cover,user-scalable=no' },
        { name: 'apple-mobile-web-app-capable', content: 'yes' },
        { name: 'apple-mobile-web-app-status-bar-style', content: 'black-translucent' },
        { name: 'theme-color', media: '(prefers-color-scheme: light)', content: '#ffffff' },
        { name: 'theme-color', media: '(prefers-color-scheme: dark)', content: '#222222' },
      ],
    },
  },
  nitro: {
    externals: {
      inline: ['tslib'] // 将 tslib 内联到构建中
    }
  },

  build: {
    transpile: ['tslib']  // 确保 tslib 被正确转译
  },
  vite: {
    build: {
      target: 'esnext',
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: true,  // 移除 console
          drop_debugger: true, // 移除 debugger
        }
      }
    },
    optimizeDeps: {
      include: [
        '@intlify/core-base',
        '@intlify/shared',
        'is-https',
      ],
    },
    plugins: [
    ]
  },

  experimental: {
    typedPages: true,
  },

  devtools: {
    vscode: {
      // 配置为 cursor 编辑器
      editor: 'cursor'
    },

    enabled: true
  },

  typescript: {
    shim: false,
  },

  features: {
    // For UnoCSS
    inlineStyles: false,
  },

  future: {
    compatibilityVersion: 4,
  },
  // 指定 Nuxt 应用程序的兼容性日期,确保应用程序在未来的 Nuxt 版本中保持稳定性
  compatibilityDate: '2025-02-28',
  devServer: {
  // https: httpsOptions,
    host: '0.0.0.0',
    port: 3000,
  },
})