/* eslint-disable nuxt/no-cjs-in-config */
const path = require('path')
const WebpackAliOSSPlugin = require('@gdyfe/webpack-alioss-plugin')
const { getEnvConfig } = require('@gdyfe/config')
const { version, name } = require('./package.json')

const env = getEnvConfig({
  server: process.env.mode,
  ic: 'ZmUtZ5WNp%XBzDw='
})

const { X_CA_STAGE, RUN_SERVER, STATIC_SOURCE_DOMAIN, STATIC_OSS_ACCESS, OSS_DOMAIN } = env
const { SERVE_PORT, BASE_URL, PROJECT_NAME, SENTRY_DSN, GTAG_ID } = env.lottery

const IS_USE_OSS = process.env.NODE_ENV === 'production' && !!STATIC_OSS_ACCESS.accessKeyId
const plugins = IS_USE_OSS
  ? [
      // 优化打包速度
      new WebpackAliOSSPlugin({
        accessKeyId: STATIC_OSS_ACCESS.accessKeyId,
        accessKeySecret: STATIC_OSS_ACCESS.accessKeySecret,
        region: STATIC_OSS_ACCESS.region,
        bucket: STATIC_OSS_ACCESS.bucket,
        prefix: PROJECT_NAME,
        limit: 10, // 备份最近 10 个版本的 oss 文件
        format: version,
        exclude: [/.*\.html$/], // 或者 /.*\.html$/,排除.html文件的上传
        deleteAll: false, // 优先匹配 format 配置项
        output: path.resolve(__dirname, './nuxt-dist/dist/client'),
        local: true // 上传打包输出目录里的文件
      })
    ]
  : []

const sentryConfig = IS_USE_OSS
  ? {
      sentry: {
        dsn: SENTRY_DSN,
        config: {
          name,
          environment: RUN_SERVER,
          release: version,
          autoSessionTracking: false
        },
        lazy: true
      }
    }
  : {}

const gtagConfig = IS_USE_OSS
  ? {
      'google-gtag': {
        id: GTAG_ID, // required
        config: {
          anonymize_ip: true,
          send_page_view: false
        }
      }
    }
  : {}

export default {
  buildDir: 'nuxt-dist',
  env: {
    ...env,
    UPLOADER_ACCESS: '',
    STATIC_OSS_ACCESS: ''
  },
  router: {
    base: BASE_URL,
    middleware: ['device'],
    extendRoutes(routes, resolve) {
      routes.push({
        path: '/',
        redirect: '/list',
        components: null
      })
      routes.push({
        path: '/index/:id',
        components: {
          default: resolve(__dirname, 'pages/index') // or routes[index].component
        }
      })
    }
  },
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: '',
    meta: [
      { charset: 'utf-8' },
      {
        name: 'viewport',
        content: 'width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover'
      },
      { hid: 'description', name: 'description', content: '' },
      { name: 'format-detection', content: 'telephone=no' },
      {
        hid: 'description',
        name: 'description',
        content: '问卷报表'
      }
    ],
    link: [
      {
        rel: 'stylesheet',
        href: `//${OSS_DOMAIN}/common/icon/iconfont.css`,
        ssr: false
      },
      { rel: 'icon', href: `//${OSS_DOMAIN}/common/img/gdy_favicon.png` },
      { rel: 'stylesheet', href: `//${STATIC_SOURCE_DOMAIN}/web-component/complaintsDeal/complaintsDeal.min.css` },
      {
        href: '//apiliveroom.dev.guangdianyun.tv/v1',
        rel: 'dns-prefetch'
      },
      {
        href: '//activity.guangdianyun.tv',
        rel: 'dns-prefetch'
      },
      {
        href: '//static-pro.guangdianyun.tv',
        rel: 'dns-prefetch'
      },
      {
        href: '//res.wx.qq.com',
        rel: 'dns-prefetch'
      },
      {
        href: '//www.googletagmanager.com',
        rel: 'dns-prefetch'
      },
      {
        rel: 'stylesheet',
        href: `//${STATIC_SOURCE_DOMAIN}/custom-plugins-library/login-center/style.css`,
      },
    ],
    script: [
      { src: `//${STATIC_SOURCE_DOMAIN}/web-component/complaintsDeal/complaintsDeal.min.js`, defer: true },
      {
        src: `//${STATIC_SOURCE_DOMAIN}/custom-plugins-library/login-center/login-center.min.js`
      },
    ]
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: ['@/assets/styles/main.less'],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
    { src: '@/plugins/routeHooks' },
    { src: '@/plugins/vant' },
    { src: '@/filters' },
    { src: '@/plugins/vueClipboard' },
    { src: '@/plugins/dmsService', ssr: false },
    { src: '@/plugins/pvStatisticalService', ssr: false },
    { src: '@/plugins/deviceId', ssr: false }
  ],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    // https://go.nuxtjs.dev/eslint
    '@nuxtjs/eslint-module',
    '@nuxtjs/style-resources'
  ],
  /* less全局变量 */
  styleResources: {
    less: './assets/styles/variable.less'
  },
  // Modules: https://go.nuxtjs.dev/config-modules
  modules: ['@nuxtjs/axios', '@nuxtjs/sentry', '@nuxtjs/google-gtag'],
  /*
   ** Axios module configuration
   ** See https://axios.nuxtjs.org/options
   */
  ...sentryConfig,
  ...gtagConfig,
  axios: {},
  server: {
    https: !process.env.NODE_ENV === 'production',
    port: SERVE_PORT, // default: 3000
    host: '0.0.0.0' // default: localhost
  },
  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    publicPath: IS_USE_OSS
      ? `//${STATIC_SOURCE_DOMAIN}/${PROJECT_NAME}/${version}`
      : `/${X_CA_STAGE.toLowerCase() || 'prod'}/`,
    transpile: [/vant.*?less/],
    babel: {
      plugins: [
        [
          'import',
          {
            libraryName: 'vant',
            style: name => {
              return `${name}/style/less.js`
            }
          },
          'vant'
        ]
      ]
    },
    postcss: {
      plugins: {
        'postcss-pxtorem': {
          rootValue: 75,
          selectorBlackList: [/^html$/, /^body$/, '.lottery-entry', 'tippy'], // 忽略转换正则匹配项
          propList: ['*']
        }
      }
    },
    plugins
  }
}
