// https://umijs.org/config/
import { defineConfig } from '@umijs/max';
import { join } from 'path';
import defaultSettings from './defaultSettings';
import proxy from './proxy';
import routes from './routes';
import authRoutes from './authRoutes';

const { REACT_APP_ENV } = process.env;
let temp = [];
function getList(tree) {
  for (let item in tree) {
    if (tree[item].routes) {
      getList(tree[item].routes);
    } else {
      temp.push(tree[item]);
    }
  }
  return temp;
}

const keepalive = getList(authRoutes)
  ?.filter((it) => {
    return it?.path && !it?.unkeepalive;
  })
  .map((it) => it.path);

//console.log(keepalive);

export default defineConfig({
  outputPath: 'package',
  hash: true,
  antd: {
    compact: true,
  },
  history: {
    type: 'hash',
  },
  manifest: {
    basePath: '/',
  },
  publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
  initialState: {},
  model: {},
  keepalive: keepalive,
  layout: {
    // https://umijs.org/zh-CN/plugins/plugin-layout
    locale: false,
    siderWidth: 208,
    ...defaultSettings,
  },
  // https://umijs.org/zh-CN/plugins/plugin-locale
  locale: {
    // default zh-CN
    default: 'zh-CN',
    antd: true,
    // default true, when it is true, will use `navigator.language` overwrite default
    baseNavigator: true,
  },
  // umi routes: https://umijs.org/docs/routing
  routes,
  access: {},
  // Theme for antd: https://ant.design/docs/react/customize-theme-cn
  theme: {
    // 如果不想要 configProvide 动态设置主题需要把这个设置为 default
    // 只有设置为 variable， 才能使用 configProvide 动态设置主色调
    // https://ant.design/docs/react/customize-theme-variable-cn
    'root-entry-name': 'variable',
  },
  ignoreMomentLocale: true,
  proxy: proxy[REACT_APP_ENV || 'dev'],
  // Fast Refresh 热更新
  fastRefresh: true,
  presets: ['umi-presets-pro'],
  openAPI: [
    {
      requestLibPath: "import { request } from '@umijs/max'",
      // 或者使用在线的版本
      // schemaPath: "https://gw.alipayobjects.com/os/antfincdn/M%24jrzTTYJN/oneapi.json"
      schemaPath: join(__dirname, 'oneapi.json'),
      mock: false,
    },
    {
      requestLibPath: "import { request } from '@umijs/max'",
      schemaPath: 'https://gw.alipayobjects.com/os/antfincdn/CA1dOm%2631B/openapi.json',
      projectName: 'swagger',
    },
  ],
  chainWebpack: function (config, { webpack }) {
    console.log(process.env.NODE_ENV);
    // 线上环境就去除console输出
    process.env.NODE_ENV === 'production' &&
      config.optimization.minimizer('terser').tap((args) => {
        args[0].terserOptions.compress.drop_console = true;
        args[0].terserOptions.compress.drop_debugger = true;
        return args;
      });
  },
});
