import { defineConfig } from '@vben/vite-config';

import { fileURLToPath, URL } from 'node:url';

import ElementPlus from 'unplugin-element-plus/vite';

const resolveLocal = (path: string) =>
  fileURLToPath(new URL(path, import.meta.url));

const exactAlias = (find: string, path: string) => ({
  find: new RegExp(`^${find.replaceAll('/', '\\/')}$`),
  replacement: resolveLocal(path),
});

export default defineConfig(async () => {
  return {
    application: {
      injectMetadata: false,
      license: false,
    },
    vite: {
      build: {
        modulePreload: false,
      },
      resolve: {
        alias: [
          exactAlias(
            '@vben-core/design',
            '../../packages/@core/base/design/src/index.ts',
          ),
          exactAlias(
            '@vben-core/design/bem',
            '../../packages/@core/base/design/src/scss-bem/bem.scss',
          ),
          exactAlias(
            '@vben-core/composables',
            '../../packages/@core/composables/src/index.ts',
          ),
          exactAlias(
            '@vben-core/form-ui',
            '../../packages/@core/ui-kit/form-ui/src/index.ts',
          ),
          exactAlias(
            '@vben-core/icons',
            '../../packages/@core/base/icons/src/index.ts',
          ),
          exactAlias(
            '@vben-core/layout-ui',
            '../../packages/@core/ui-kit/layout-ui/src/index.ts',
          ),
          exactAlias(
            '@vben-core/menu-ui',
            '../../packages/@core/ui-kit/menu-ui/src/index.ts',
          ),
          exactAlias(
            '@vben-core/popup-ui',
            '../../packages/@core/ui-kit/popup-ui/src/index.ts',
          ),
          exactAlias(
            '@vben-core/preferences',
            '../../packages/@core/preferences/src/index.ts',
          ),
          exactAlias(
            '@vben-core/shadcn-ui',
            '../../packages/@core/ui-kit/shadcn-ui/src/index.ts',
          ),
          exactAlias(
            '@vben-core/shared/cache',
            '../../packages/@core/base/shared/src/cache/index.ts',
          ),
          exactAlias(
            '@vben-core/shared/color',
            '../../packages/@core/base/shared/src/color/index.ts',
          ),
          exactAlias(
            '@vben-core/shared/constants',
            '../../packages/@core/base/shared/src/constants/index.ts',
          ),
          exactAlias(
            '@vben-core/shared/global-state',
            '../../packages/@core/base/shared/src/global-state.ts',
          ),
          exactAlias(
            '@vben-core/shared/store',
            '../../packages/@core/base/shared/src/store.ts',
          ),
          exactAlias(
            '@vben-core/shared/utils',
            '../../packages/@core/base/shared/src/utils/index.ts',
          ),
          exactAlias(
            '@vben-core/tabs-ui',
            '../../packages/@core/ui-kit/tabs-ui/src/index.ts',
          ),
          exactAlias(
            '@vben-core/typings',
            '../../packages/@core/base/typings/src/index.ts',
          ),
          exactAlias(
            '@vben/access',
            '../../packages/effects/access/src/index.ts',
          ),
          exactAlias(
            '@vben/common-ui',
            '../../packages/effects/common-ui/src/index.ts',
          ),
          exactAlias(
            '@vben/common-ui/es/tippy',
            '../../packages/effects/common-ui/src/components/tippy/index.ts',
          ),
          exactAlias(
            '@vben/common-ui/es/api-component',
            '../../packages/effects/common-ui/src/components/api-component/index.ts',
          ),
          exactAlias(
            '@vben/common-ui/es/login-expired-modal',
            '../../packages/effects/common-ui/src/ui/authentication/login-expired-modal.vue',
          ),
          exactAlias(
            '@vben/constants',
            '../../packages/constants/src/index.ts',
          ),
          exactAlias(
            '@vben/hooks',
            '../../packages/effects/hooks/src/index.ts',
          ),
          exactAlias('@vben/icons', '../../packages/icons/src/index.ts'),
          exactAlias(
            '@vben/layouts',
            '../../packages/effects/layouts/src/index.ts',
          ),
          exactAlias(
            '@vben/layouts/preferences-blocks',
            '../../packages/effects/layouts/src/widgets/preferences/blocks/index.ts',
          ),
          exactAlias(
            '@vben/layouts/es/lock-screen',
            '../../packages/effects/layouts/src/widgets/lock-screen/index.ts',
          ),
          exactAlias(
            '@vben/layouts/es/user-dropdown',
            '../../packages/effects/layouts/src/widgets/user-dropdown/index.ts',
          ),
          exactAlias('@vben/locales', '../../packages/locales/src/index.ts'),
          exactAlias(
            '@vben/preferences',
            '../../packages/preferences/src/index.ts',
          ),
          exactAlias(
            '@vben/request',
            '../../packages/effects/request/src/index.ts',
          ),
          exactAlias('@vben/stores', '../../packages/stores/src/index.ts'),
          exactAlias('@vben/styles', '../../packages/styles/src/index.ts'),
          exactAlias(
            '@vben/styles/ele',
            '../../packages/styles/src/ele/index.css',
          ),
          exactAlias('@vben/types', '../../packages/types/src/index.ts'),
          exactAlias('@vben/utils', '../../packages/utils/src/index.ts'),
        ],
      },
      plugins: [
        ElementPlus({
          format: 'esm',
        }),
      ],
      server: {
        proxy: {
          '/basic-api': {
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/basic-api/, ''),
            target: 'http://localhost:8000',
            ws: true,
          },
          '/ws': {
            changeOrigin: true,
            target: 'ws://localhost:8000',
            ws: true,
          },
        },
      },
    },
  };
});
