/** @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",
      // Keep PrimeVue on the same @primevue/core / @primeuix/styled instances as bootCore.
      // Pre-bundling primevue/* caused unstyled buttons/inputs (theme inject on a duplicate Symbol).
      "primevue",
      "@primevue/core",
      "@primeuix/styled",
      "@primeuix/themes",
    ],
    // quill-delta is CJS; because @maestro/core is excluded from pre-bundling its
    // transitive deps are not crawled. Force esbuild to convert them to ESM so that
    // quill's `import Delta from 'quill-delta'` resolves the default export correctly.
    include: ["quill", "quill-delta"],
  },
  plugins: [
    laravel({
      input: [
        "resources/css/app.css",
        "resources/js/app.js",
        "resources/js/public-home.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",
      },
    },
  },
});
