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

import vue from "@vitejs/plugin-vue";
import vueDevTools from "vite-plugin-vue-devtools";
import tailwindcss from "@tailwindcss/vite";
import type { ConfigEnv, UserConfig } from "vite";
import { loadEnv } from "vite";

import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import MotionResolver from "motion-v/resolver";

const root = process.cwd();

// export default defineConfig({
//   server: {
//     host: "0.0.0.0", // 监听所有网络接口
//     port: 5173, // 可选：指定端口
//   },
//   base: "./",
//   plugins: [
//     vue(),
//     vueDevTools(),
//     tailwindcss(),
//     AutoImport({
//       resolvers: [ElementPlusResolver()],
//     }),
//     Components({
//       dts: true,
//       resolvers: [ElementPlusResolver(), MotionResolver()],
//     }),
//   ],
//   resolve: {
//     alias: {
//       "@": fileURLToPath(new URL("./src", import.meta.url)),
//       "@components": fileURLToPath(
//         new URL("./src/components", import.meta.url)
//       ),
//       "@assets": fileURLToPath(new URL("./src/assets", import.meta.url)),
//       "@api": fileURLToPath(new URL("./src/api", import.meta.url)),
//       "@utils": fileURLToPath(new URL("./src/utils", import.meta.url)),
//       "@types": fileURLToPath(new URL("./src/types", import.meta.url)),
//       "@stores": fileURLToPath(new URL("./src/stores", import.meta.url)),
//       "@hooks": fileURLToPath(new URL("./src/hooks", import.meta.url)),
//     },
//   },
// });

export default ({ command, mode }: ConfigEnv): UserConfig => {
  let env = {} as any;
  const isBuild = command === "build";
  if (!isBuild) {
    env = loadEnv(
      process.argv[3] === "--mode" ? process.argv[4] : process.argv[3],
      root,
    );
  } else {
    env = loadEnv(mode, root);
  }

  return {
    server: {
      host: "0.0.0.0", // 监听所有网络接口
      port: 5174, // 可选：指定端口
    },
    base: "./",
    plugins: [
      vue(),
      vueDevTools(),
      tailwindcss(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        dts: true,
        resolvers: [ElementPlusResolver(), MotionResolver()],
      }),
    ],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
        "@components": fileURLToPath(
          new URL("./src/components", import.meta.url),
        ),
        "@assets": fileURLToPath(new URL("./src/assets", import.meta.url)),
        "@api": fileURLToPath(new URL("./src/api", import.meta.url)),
        "@utils": fileURLToPath(new URL("./src/utils", import.meta.url)),
        "@types": fileURLToPath(new URL("./src/types", import.meta.url)),
        "@stores": fileURLToPath(new URL("./src/stores", import.meta.url)),
        "@hooks": fileURLToPath(new URL("./src/hooks", import.meta.url)),
      },
    },
  };
};
