/** @format */

import process from 'node:process'
import { sitemapConfig } from './utils/sitemap.config'

const isDev = process.env.NODE_ENV === 'development'
export default defineNuxtConfig({
  ssr: true,
  app: {
    pageTransition: { name: 'page', mode: 'out-in', onBeforeEnter: () => { window.scrollTo(0, 0) } },
    // layoutTransition: { name: 'layout', mode: 'out-in', onBeforeEnter() { window.scrollTo(0, 0) } },
    head: {
      htmlAttrs: {
        lang: 'en',
      },
    },
  },
  modules: [
    '@unocss/nuxt',
    '@vueuse/nuxt',
    '@nuxt/image',
    '@nuxtjs/i18n',
    '@nuxtjs/html-validator',
    '@element-plus/nuxt',
    '@pinia/nuxt',
    '@pinia-plugin-persistedstate/nuxt',
    'nuxt-svgo',
    '@nuxtjs/sitemap',
  ],
  css: [
    'element-plus/dist/index.css',
    'element-plus/theme-chalk/display.css',
    '@/assets/style/reset.css',
    '@/assets/style/public.css',
    '@/assets/style/variables.css',
    '@/assets/style/common.less',
  ],
  experimental: {
    inlineSSRStyles: false,
    viewTransition: true,
    renderJsonPayloads: true,
  },
  routeRules: {
    '/**': isDev
      ? {}
      : {
          cache: { swr: true, maxAge: 120, staleMaxAge: 60, headersOnly: true },
        },
  },

  runtimeConfig: {
    public: {
      apiBase: process.env.MY_ENV_BASE,
      scrollToTop: true,
    },
  },
  devtools: {
    enabled: true,
  },
  // image: {
  //   provider: 'proxy',
  //   providers: {
  //     proxy: {
  //       provider: 'ipx',
  //       options: {
  //         baseURL: `${apiBaseUrl}/ipx`,
  //       },
  //     },
  //   },
  // },
  nitro: {
    routeRules: {
      '/**': { isr: false },
    },
    devProxy: {
      '/jeecgboot': {
        target: process.env.MY_ENV_DEV_URL,
        changeOrigin: true,
      },
    },
  },
  i18n: {
    detectBrowserLanguage: {
      useCookie: true,
      fallbackLocale: 'en',
    },
    strategy: 'no_prefix',
    locales: [
      {
        code: 'en',
        name: 'English',
        file: 'en.json',
      },
      {
        code: 'zh-CN',
        name: '简体中文',
        file: 'zh-CN.json',
      },
    ],
    lazy: true,
    langDir: 'internationalization',
    defaultLocale: 'en',
  },
  htmlValidator: {
    usePrettier: false,
    logLevel: 'verbose',
    failOnError: false,
    /** A list of routes to ignore (that is, not check validity for). */
    ignore: [/\.(xml|rss|json)$/],
    options: {
      extends: [
        'html-validate:document',
        'html-validate:recommended',
        'html-validate:standard',
      ],
      rules: {
        'svg-focusable': 'off',
        'no-unknown-elements': 'error',
        // Conflicts or not needed as we use prettier formatting
        'void-style': 'off',
        'no-trailing-whitespace': 'off',
        // Conflict with Nuxt defaults
        'require-sri': 'off',
        'attribute-boolean-style': 'off',
        'doctype-style': 'off',
        // Unreasonable rule
        'no-inline-style': 'off',
      },
    },
  },
  sitemap: {
    urls: async () => {
      // 只生成静态路由
      const staticRoutes = sitemapConfig.staticRoutes.map(route => ({
        ...route,
        loc: `${sitemapConfig.hostname}${route.loc}`,
        lastmod: new Date().toISOString(),
      }))
      // 生成动态路由
      const dynamicRoutes = await sitemapConfig.generateDynamicRoutes()
      const dynamics = dynamicRoutes.map(route => ({
        ...route,
        loc: route.loc,
        lastmod: new Date().toISOString(),
      }))
      // 合并路由
      const routes = [...staticRoutes, ...dynamics]
      return routes
    },
    defaults: {
      changefreq: 'daily',
      priority: 0.5,
      lastmod: new Date().toISOString(),
    },
    exclude: [
      '/404',
    ],
    // exclude all app sources
    excludeAppSources: true,
    debug: true,
  },
})
