function getOrigin(url: string) {
  try {
    return new URL(url).origin
  } catch {
    return ''
  }
}

const tenantId = process.env.NUXT_PUBLIC_TENANT_ID || '5'
const serverApiBase =
  process.env.NUXT_PUBLIC_SERVER_API_BASE ||
  process.env.NUXT_PUBLIC_SERVER_API ||
  process.env.SERVER_API_URL ||
  process.env.VITE_SERVER_URL ||
  'https://server.websoft.top/api'
const modulesApiBase =
  process.env.NUXT_PUBLIC_MODULES_API_BASE ||
  process.env.MODULES_API_URL ||
  process.env.NUXT_PUBLIC_CMS_API_BASE ||
  process.env.API_BASE ||
  'https://mp-api.websoft.top/api'
const appApiBase =
  process.env.NUXT_PUBLIC_APP_API_BASE ||
  'https://websopy-api.websoft.top'
const mpApiBase =
  process.env.NUXT_PUBLIC_MP_API_BASE ||
  'https://mp-api.websoft.top'
const fileServerBase =
  process.env.NUXT_PUBLIC_FILE_SERVER_BASE ||
  process.env.FILE_SERVER_BASE ||
  getOrigin(serverApiBase) ||
  'https://server.websoft.top'

const WATCH_IGNORED = [
  '**/.git/**',
  '**/node_modules/**',
  '**/.nuxt/**',
  '**/.output/**',
  '**/dist/**',
  '**/coverage/**',
  '**/.cache/**'
]

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  compatibilityDate: '2025-07-15',
  devtools: { enabled: true },
  modules: ['@nuxt/content', '@nuxtjs/tailwindcss', '@nuxtjs/i18n', './modules/fix-tailwind-postcss'],
  app: {
    head: {
      titleTemplate: '%s - websopy',
      meta: [
        { charset: 'utf-8' },
        { name: 'viewport', content: 'width=device-width, initial-scale=1' },
        { name: 'robots', content: 'index,follow' }
      ],
      link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]
    }
  },
  tailwindcss: {
    cssPath: '~/assets/css/tailwind.css',
    configPath: 'tailwind.config.cjs'
  },
  experimental: { appManifest: false },

  // i18n 配置
  i18n: {
    locales: [
      { code: 'zh-CN', name: '简体中文', file: 'zh-CN.ts' },
      { code: 'en', name: 'English', file: 'en.ts' }
    ],
    defaultLocale: 'zh-CN',
    langDir: '../locales',
    strategy: 'no_prefix',
    detectBrowserLanguage: {
      useCookie: true,
      cookieKey: 'i18n_locale',
      redirectOn: 'root',
      alwaysRedirect: true,
      fallbackLocale: 'zh-CN'
    }
  },
  runtimeConfig: {
    public: {
      tenantId,
      serverApiBase,
      modulesApiBase,
      appApiBase,
      mpApiBase,
      fileServerBase,
      templateId:
        process.env.NUXT_PUBLIC_TEMPLATE_ID || tenantId,

      // Backward-compatible names (existing code may rely on them)
      ServerApi: serverApiBase,
      ApiBase: modulesApiBase,
      TenantId: tenantId
    }
  },
  css: ['ant-design-vue/dist/reset.css', '~/assets/css/tailwind.css', '~/assets/css/admin-common.css'],
  vite: {
    server: {
      watch: {
        ignored: WATCH_IGNORED
      }
    },
    optimizeDeps: {
      include: ['dayjs']
    }
  },
  nitro: {
    watchOptions: {
      ignored: WATCH_IGNORED
    }
  },
  watchers: {
    chokidar: {
      ignored: WATCH_IGNORED
    }
  }
})
