const slash = require('slash2');
const proxy = require('./proxy');
const defaultSetting = require('./defaultSetting');
const cesiumBuild = '../node_modules/cesium/Build/Cesium';
const pkgName = require('../package.json').name;
// eslint-disable-next-line import/order
const { REACT_APP_ENV } = process.env;
// eslint-disable-next-line import/order
const path = require('path');
const CESIUM_BASE_URL = `/${pkgName}`;
module.exports = {
  // mfsu: {},
  esbuild: {},
  webpack5: {},
  workerLoader: true,
  base: '/civbase',
  title: '熊猫智慧城市监控管理解决方案',
  hash: true,
  layout: {
    siderWidth: '145px',
    locale: true,
    ...defaultSetting,
  },
  fastRefresh: true,
  useHash: true,
  locale: {
    default: 'zh-CN',
    antd: true,
    baseNavigator: true,
  },
  dynamicImport: {
    loading: '@ant-design/pro-layout/es/PageLoading',
  },
  // theme: {
  //   '@primary-color': '#ff9600',
  // },
  ignoreMomentLocale: true,
  nodeModulesTransform: {
    type: 'none',
    exclude: [
      '@wisdom-map/arcgismap',
      '@wisdom-map/Amap',
      '@wisdom-map/Map',
      'swagger-ui-react',
      'Cesium',
    ],
  },
  proxy: proxy[REACT_APP_ENV || 'dev'],
  // openAPI: {
  //   requestLibPath: "import { request } from '@wisdom-utils/utils'",
  //   schemaPath: 'http://192.168.10.150:8777/Publish/OMS/swagger/v1/swagger.json',
  //   mock: false,
  // },
  postcssLoader: {},
  styleLoader: {},
  cssLoader: {
    modules: {
      getLocalIdent: (context, _, localName) => {
        if (
          context.resourcePath.includes('node_modules') ||
          context.resourcePath.includes('ant.design.pro.less') ||
          context.resourcePath.includes('global.less') ||
          context.resourcePath.includes('ThemeColor.less')
        ) {
          return localName;
        }

        const match = context.resourcePath.match(/(src(.*))/);

        if (match && match[1]) {
          const antdProPath = match[1].replace('.less', '');
          const arr = slash(antdProPath)
            .split('/')
            .map(a => a.replace(/([A-Z])/g, '-$1'))
            .map(a => a.toLowerCase());
          return `panda-console-base${arr.join('-')}-${localName}`.replace(/--/g, '-');
        }
        return localName;
      },
    },
  },
  lessLoader: {
    modifyVars: {
      // hack: `true; @import "~@/global.less";`,
      '@ant-prefix': 'panda-console-base',
      '@iconPrefixCls': 'panda-console-base-icon',
    },
    javascriptEnabled: true,
  },
  copy: [
    {
      from: path.join(pkgName, cesiumBuild, './Workers'),
      to: 'Workers',
    },
    {
      from: path.join(pkgName, cesiumBuild, 'Assets'),
      to: 'Assets',
    },
    {
      from: path.join(pkgName, cesiumBuild, 'Widgets'),
      to: 'Widgets',
    },
    {
      from: path.join(pkgName, cesiumBuild, 'ThirdParty'),
      to: 'ThirdParty',
    },
  ],
  define: {
    CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL).replace(/\"/g, ''),
  },
  alias: {
    cesium: path.resolve(__dirname, cesiumBuild),
  },
  workerLoader: {},
  copy: [
    {
      from: path.join(pkgName, cesiumBuild, './Workers'),
      to: 'Workers',
    },
    {
      from: path.join(pkgName, cesiumBuild, 'Assets'),
      to: 'Assets',
    },
    {
      from: path.join(pkgName, cesiumBuild, 'Widgets'),
      to: 'Widgets',
    },
    {
      from: path.join(pkgName, cesiumBuild, 'ThirdParty'),
      to: 'ThirdParty',
    },
  ],
  define: {
    CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL).replace(/\"/g, ''),
  },
  alias: {
    cesium: path.resolve(__dirname, cesiumBuild),
  },

  // esbuild: {}
};
