// import { createSplitChunkOutput } from 'utils4u/rollup';

import { fileURLToPath, URL } from 'node:url';
import { createViteProxy } from 'utils4u/vite';
import { defineConfig, loadEnv } from 'vite';

import { cesiumBaseUrl, Plugins } from './vite.config.plugins';

const primevuecomponents = await (async () => {
  const { components } = await import('@primevue/metadata');
  return components.map((c) => c.from).filter((c) => c !== undefined);
})();

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const isBuild = command === 'build';
  const env = loadEnv(mode, process.cwd());

  return {
    // https://cn.vite.dev/config/dep-optimization-options
    // ???
    optimizeDeps: {
      include: [
        ...primevuecomponents,
        '@primeuix/themes',
        '@primeuix/themes/lara',
        'class-variance-authority',
        'clsx',
        'tailwind-merge',
        'reka-ui',
        'axios',
        '@ant-design/icons-vue',
        'ant-design-vue/es',
        'p5',
        '@splinetool/runtime',
        'satellite.js',
        'ts-enum-util',
        'unplugin-vue-router',
        'unplugin-vue-router/runtime',
        'unplugin-vue-router/data-loaders/basic',
        'unplugin-vue-router/data-loaders/pinia-colada',
      ],
      exclude: ['quill', 'chart.js/auto'],
    },
    base: env.VITE_BASE,
    build: {
      minify: 'terser',
      rollupOptions: {
        onwarn: (warning, warn) => {
          if (warning.code === 'EMPTY_BUNDLE') return;
          if (warning.code === 'EVAL' && warning.id?.includes('node_modules/eruda')) return;
          if (warning.code === 'EVAL' && warning.id?.includes('node_modules/mockjs')) return;
          warn(warning);
        },

        // https://cn.rollupjs.org/configuration-options/#output-assetfilenames
        // output: env.VITE_SPLIT_CHUNKS === 'true' ? (await import('utils4u/rollup')).createSplitChunkOutput() : undefined,
        output: {
          // minifyInternalExports: false,
          // manualChunks: {
          //   'vendor/utils4u': ['utils4u', 'utils4u/vue-use', 'utils4u/primevue'],
          //   'vendor/vue': ['vue'],
          //   'vendor/primevue': ['primevue'],
          //   'vendor/faker-js': ['@faker-js/faker'],
          //   'vendor/mockjs': ['mockjs'],
          //   'vendor/axios': ['axios', 'alova', '@alova/adapter-axios'],
          //   // 'vendor/nprogress': ['nprogress'],
          //   // 'vendor/formkit': ['@formkit/auto-animate'],
          // },
          manualChunks: {
            'vendor/Cesium': ['cesium'],
          },
          // advancedChunks: {
          //   groups: [
          //     {
          //       name: 'vendor/cesium',
          //       test: 'cesium',
          //     },
          //   ],
          // },
        },
      },
      sourcemap: mode !== 'production' || env.VITE_SOURCE_MAP === 'true',
    },
    define: {
      $__DEV__: JSON.stringify(!isBuild),
      CESIUM_BASE_URL: JSON.stringify(`/${cesiumBaseUrl}`),
    },
    plugins: Plugins(),
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('src', import.meta.url)),
      },
    },
    server: {
      allowedHosts: ['.nwct.dev'],
      proxy: createViteProxy(),
    },
  };
});
