/// <reference types="vitest" />

import { cloudflare } from "@cloudflare/vite-plugin";
import tailwindcss from "@tailwindcss/vite";
import react from "@vitejs/plugin-react";
import { visualizer } from "rollup-plugin-visualizer";
import { defineConfig, type PluginOption } from "vite";
import { VitePWA } from "vite-plugin-pwa";
import tsconfigPaths from "vite-tsconfig-paths";

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const plugins: PluginOption[] = [
    react(),
    visualizer() as PluginOption,
    tailwindcss(),
    tsconfigPaths(),
  ];

  plugins.push(
    VitePWA({
      registerType: "autoUpdate",
      injectRegister: false,
      includeAssets: [
        "icon.jpg",
        "logo_dark.png",
        "logo_white.png",
      ],
      manifest: false,
      workbox: {
        globPatterns: ["**/*.{js,css,html,ico,png,jpg,svg,woff,woff2}"],
        globIgnores: ["**/*.map"],
        navigateFallback: "index.html",
        maximumFileSizeToCacheInBytes: 5 * 1024 * 1024,
      },
    })
  );

  // Only include cloudflare plugin when not in test mode
  if (mode !== "test" && process.env.VITEST !== "true") {
    plugins.push(cloudflare({ inspectorPort: 9230 }));
  }

  return {
    plugins,
    server: {
      cors: false,
    },
    define: process.env.VITEST
      ? {
          "import.meta.env.VITE_SUPABASE_URL": JSON.stringify("https://test.supabase.co"),
          "import.meta.env.VITE_SUPABASE_ANON_KEY": JSON.stringify("test-anon-key"),
        }
      : undefined,
    test: {
      globals: true,
      environment: "jsdom",
      setupFiles: "./src/setupTests.ts",
    },
  };
});
