import webpack from 'webpack'
export default {
  loading: false,
  server: {
    port: process.env.NODE_ENV === 'development' ? 9877 : 3000,
    host: '0.0.0.0',
    public: '0.0.0.0',
  },
  watchers: {
    webpack: {
      aggregateTimeout: 300,
      poll: 1000,
    },
  },
  head: {
    title: ' ',
    htmlAttrs: {
      lang: 'en',
    },
    meta: [
      { charset: 'utf-8' },
      {
        name: 'google-site-verification',
        content: 'fQgAl9rz9JmPUq85LR8zGTHR7KxbCz19V1pBqiQc27Q',
      },
      { name: 'viewport', content: 'width=1400, initial-scale=0.5' },
      {
        hid: 'description',
        name: 'description',
        content: 'Promotional Products, Lanyards, Flash Drives, Mousemats.',
      },
      { hid: 'keywords', name: 'keywords', content: 'PromoCollection' },
      { name: 'format-detection', content: 'telephone=no' },
    ],
    link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
    script: [
      { src: '//yun.baoxiaohe.com/openflatform/sdk/v2.2/staticwwnf5q1r.js' },
    ],
  },

  css: [
    'element-ui/lib/theme-chalk/index.css',
    '@/assets/iconfont/iconfont.css',
    '@/assets/css/reset.scss',
    '@/assets/css/common.scss',
    '@/assets/css/element.scss',
    '@/assets/css/flex-custom.scss',
  ],
  plugins: [
    '@/plugins/element-ui',
    '@/plugins/axios.js',
    '@/plugins/router.js',
    '@/plugins/utils.js',
    '@/plugins/oss.js',
    { src: '@/assets/iconfont/iconfont.js', ssr: false },
    { src: '@/assets/googlemap.js', ssr: false },
    ...(process.env.NODE_ENV === 'production' ||
    process.env.NODE_ENV === 'development'
      ? [
          { src: '@/plugins/clarity.js', mode: 'client' },
          { src: '@/plugins/baidu-analytics.js', mode: 'client' },
          { src: '@/plugins/google-analytics.js', mode: 'client' },
        ]
      : []),
  ],
  components: true,
  buildModules: ['@nuxt/postcss8'],
  modules: ['@nuxtjs/axios', '@nuxtjs/proxy', 'cookie-universal-nuxt'],
  axios: {
    proxy: true,
    prefix: '',
  },
  proxy: {
    '/api': {
      target:
        process.env.NODE_ENV === 'development'
          ? 'http://13.55.42.195:9003'
          : 'http://127.0.0.1:8082',
      changeOrigin: true,
      pathRewrite: {
        '^/api': '',
      },
    },
    '/c-api': {
      target:
        process.env.NODE_ENV === 'development'
          ? 'http://13.55.42.195:10172'
          : 'http://127.0.0.1:8083',
      changeOrigin: true,
      pathRewrite: {
        '^/c-api': '',
      },
    },
  },
  build: {
    extend(config, ctx) {
      if (ctx.isClient) {
        // Enable Source Maps for client-side code
        config.devtool = 'source-map'
      }
    },
    // postcss: {
    //   plugins: {
    //     'postcss-pxtorem': {
    //       rootValue: 16,
    //       propList: ['*']
    //     }
    //   }
    // },
    extractCSS: true,
    transpile: [/^element-ui/],
    plugins: [
      new webpack.ProvidePlugin({
        _: 'lodash',
      }),
    ],
  },
  // https://www.nuxtjs.cn/api/configuration-router#scrollBehavior
  router: {
    middleware: 'redirect',
    scrollBehavior(to, from, savedPosition) {
      // if the returned position is falsy or an empty object,
      // will retain current scroll position.
      let position = false

      // 自定义行为
      const reg = /\/category/
      if ((reg.test(to.path) && reg.test(from.path)) || to.path !== from.path) {
        // 在5种分类页间跳转 或者 同组件路由跳转时, 重置滚动距离
        position = { x: 0, y: 0 }
      } else if (savedPosition) {
        position = savedPosition
      }
      if (to.hash) {
        let hash = to.hash
        // CSS.escape() is not supported with IE and Edge.
        if (
          typeof window.CSS !== 'undefined' &&
          typeof window.CSS.escape !== 'undefined'
        ) {
          hash = '#' + window.CSS.escape(hash.substr(1))
        }
        try {
          if (document.querySelector(hash)) {
            // scroll to anchor by returning the selector
            position = { selector: hash }
          } else {
            // 部分情况下, 点击带hash的路由链接跳转会没法正确识别到hash, 需要用这种方式处理.
            return new Promise(resolve => {
              window.$nuxt.$once('triggerScroll', () => {
                position = { selector: hash }
                resolve(position)
              })
            })
          }
        } catch (e) {
          console.warn(
            'Failed to save scroll position. Please add CSS.escape() polyfill (https://github.com/mathiasbynens/CSS.escape).'
          )
        }
        return position
      } else {
        return position
      }
    },
  },
}
