/** @maestro-core-managed — reset via `php artisan core:install --force` */
import { fileURLToPath, URL } from "node:url";
import { PrimeVueResolver } from "@primevue/auto-import-resolver";
import tailwindcss from "@tailwindcss/vite";
import vue from "@vitejs/plugin-vue";
import laravel from "laravel-vite-plugin";
import Components from "unplugin-vue-components/vite";
import { defineConfig } from "vite";

/**
 * Skip node_modules except @maestro/core (npm flat layout + pnpm .pnpm/@maestro+core@…).
 * unplugin-vue-components exclude must be regex/string — not a predicate function.
 */
const excludeNodeModulesExceptMaestro =
  /[/\\]node_modules[/\\](?!.*@maestro[/\\+]core)/;

export default defineConfig({
  /**
   * Do not pre-bundle @maestro/core: Vite would inline a second Vue runtime while
   * SFCs still import `vue` from the app, causing resolveComponent warnings and a blank UI.
   */
  optimizeDeps: {
    exclude: ["@maestro/core"],
  },
  plugins: [
    laravel({
      input: ["resources/css/app.css", "resources/js/app.js"],
      refresh: true,
    }),
    vue(),
    tailwindcss(),
    Components({
      resolvers: [PrimeVueResolver()],
      dirs: [],
      exclude: [
        excludeNodeModulesExceptMaestro,
        /[/\\]\.git[/\\]/,
        /[/\\]\.nuxt[/\\]/,
      ],
    }),
  ],
  resolve: {
    dedupe: ["vue", "vue-router"],
    alias: {
      "@": fileURLToPath(new URL("./resources/js", import.meta.url)),
      "@core": fileURLToPath(
        new URL("./node_modules/@maestro/core/resources/js", import.meta.url),
      ),
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        api: "modern-compiler",
      },
    },
  },
});
