// 使用工具提供的nuxtConfigExtend工具
import { nuxtConfigExtend } from "@mega-apps/cli";

// 引入Base中的nuxt.config.配置
import {
  generateBaseConfig,
  def,
  isProductEnv,
} from "@mega-apps/cli/config/nuxt.config";
import CompressionPlugin from "compression-webpack-plugin";
import ThemeColorReplacer from "@mega-apps/webpack-theme-color-replacer";
import { getThemeColors, modifyVars } from "./utils/theme/update";
import { resolveCss } from "./utils/theme/replacer";

// api前缀
import ApiConstant from "./api/ApiConstant";

// eslint-disable-next-line
const { resolve } = require("path");

const baseCfg = generateBaseConfig({
  supportESLint: false,
  supportStyleLint: false,
  supportTailwindCSS: true,
  forceUsePostCssV8: true,
});

// 构造新的 nuxt.config
const config = nuxtConfigExtend(
  [
    def(true, {
      env: {
        SSO: process.env.SSO,
        ENABLE_THEME_REPLACE: process.env.ENABLE_THEME_REPLACE,
        IS_PRODUCT_ENV: process.env.IS_PRODUCT_ENV || isProductEnv ? "1" : "0",
      },

      // Global CSS: https://go.nuxtjs.dev/config-css
      css: [
        "animate.css/animate.min.css",
        { src: "~theme/index.less", lang: "less" },
        { src: "~assets/less/index.less", lang: "less" },
      ],

      // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
      // 注意次序：插件从上而下顺序执行
      plugins: [
        "@/plugins/hooks/nuva/index.js",
        "@/plugins/antd-ui",
        "@/plugins/component", // 全局组件
        "@/plugins/i18n",
        "@/plugins/axios",
        "@/plugins/setting",
        "@/plugins/generateRouter",
        "@/plugins/event-bus",
        "@/plugins/mock",
        "@/plugins/mixins",
        "@/plugins/webpack/svg-icon",
        { src: "@/plugins/vuedraggable", ssr: false },
        { src: "@/plugins/vue-grid-layout", ssr: false },
      ],

      // Axios module configuration: https://go.nuxtjs.dev/config-axios
      proxy: {
        [ApiConstant.PREFIX_CORE]: {
          target: "http://nuva-qa.auodigitech.com",
          changeOrigin: true,
          pathRewrite: {
            [`^${ApiConstant.PREFIX_CORE}`]: ApiConstant.PREFIX_CORE,
          },
        },
        "/api": {
          target: "http://nuva-qa.auodigitech.com",
          changeOrigin: true,
          pathRewrite: {
            "^/api": "/api",
          },
        },
        "/ws": {
          target: "http://192.168.101.66:30001",
          ws: true,
          secure: false,
          changeOrigin: true,
          pathRewrite: {
            "/ws": "",
          },
        },
        "/job-server/api": {
          target: "http://192.168.101.54:8081",
        },
        "/file": {
          target: "http://192.168.101.54:8187",
        },
      },
      // Extend default values for Nuxt.js modules
      auth: {
        // 重定向
        redirect: false,
        // localStorage 的弊端，没有办法控制有效期，生产中，将只使用cookie来获取
        localStorage: false,
        // 全局Token设置
        token: {
          prefix: "_token.",
        },
        // 刷新Token设置
        refresh_token: {
          prefix: "_refresh_token.",
        },
        // 策略设置
        strategies: {
          local: {
            endpoints: {
              login: {
                url: `${ApiConstant.PREFIX_CORE}/oauth/token`,
                method: "post",
                propertyName: "access_token",
              },
              logout: {
                url: `${ApiConstant.PREFIX_CORE}/oauth/logout`,
                method: "get",
              },
              user: {
                url: `${ApiConstant.PREFIX_CORE}/api/sys/user/info`,
                method: "get",
                propertyName: "data",
              },
            },
          },
        },
        // Cookie存储设置
        cookie: {
          prefix: "auth.",
          options: {
            path: "/",
          },
        },
      },

      build: {
        plugins: [
          ...def(process.env.ENABLE_THEME_REPLACE === "1", [
            new ThemeColorReplacer({
              fileName: "css/theme-colors-[contenthash:8].css",
              matchColors: getThemeColors(),
              injectCss: true,
              resolveCss,
            }),
          ]),
          // 配置webpack插件实现代码压缩
          ...def(process.env.IS_PRODUCT_ENV === "1", [
            // 配置webpack插件实现代码压缩
            new CompressionPlugin({
              test: /\.js$|\.html$|\.png$|\.svg$|\.ttf$|\.woff$|\.css/, // 匹配文件名
              threshold: 10240, // 对超过10kb的数据进行压缩
              deleteOriginalAssets: false, // 是否删除原文件
            }),
          ]),
        ],
        loaders: {
          less: {
            lessOptions: {
              javascriptEnabled: true,
              modifyVars: modifyVars(),
            },
          },
        },

        // eslint-disable-next-line
        extend(config, options) {
          const srcDir = resolve(
            process.cwd(),
            "node_modules/@mega-design/icons/svg"
          );
          // set svg-sprite-loader
          const svgRule = config.module.rules.find((rule) =>
            rule.test.test(".svg")
          );
          svgRule.exclude = [srcDir];

          // Includes /assets/icons/svg for svg-sprite-loader
          config.module.rules.push({
            test: /\.svg$/,
            include: [srcDir],
            loader: "svg-sprite-loader",
            options: {
              symbolId: "icon-[name]",
            },
          });
        },
      },

      momDebug: false,
      // 将你需要参与资源整合的文件夹名称添加到下面， 例如：下面的代码
      momModuleIncludeDirNames: ["theme"],
      // 可以指定哪些目录作为Mom的模块用于集成构建，更多细节，查看 `momModules` 部分
      momModules: [
        {
          // Mom Module 的路径
          path: "node_modules/@mega-apps/nuva-business-ui",
          // Mom Module 的特殊设置，可以过滤不需要的文件或文件夹，及处理行为
          options: {
            // 子文件名称，参见Mom Module的目录结构
            components: {
              enabled: true,
            },
          },
        },
      ],
      // 端口配置
      server: {
        port: 3005, // default: 3000
        host: "0.0.0.0", // default: localhost
      },
    }),
  ],
  baseCfg
);

// console.log("-----> config =", config);
// process.exit(1);

export default config;
