import { resolve } from "node:path"
import ui from "@nuxt/ui/vite"
import tailwindcss from "@tailwindcss/vite"
import vue from "@vitejs/plugin-vue"
import { defineConfig } from "vite"

const PKGS_EDITOR = "../../packages/editor"

export default defineConfig({
  base: process.env.BASE_URL || "/",
  plugins: [
    vue(),
    tailwindcss(),
    ui({
      scanPackages: ["@enesis/editor"],
    }),
  ],
  build: {
    sourcemap: false,
  },
  optimizeDeps: {
    include: [
      "prosemirror-state",
      "prosemirror-view",
      "prosemirror-model",
      "prosemirror-transform",
      "prosemirror-gapcursor",
      "prosemirror-keymap",
    ],
    exclude: ["node"],
  },
  resolve: {
    alias: [
      // Editor source uses @/ prefix for src/ imports
      {
        find: /^@\//,
        replacement: `${resolve(__dirname, PKGS_EDITOR, "src")}/`,
      },
      {
        find: "@enesis/editor",
        replacement: resolve(__dirname, `${PKGS_EDITOR}/src/index.ts`),
      },
      {
        find: "prosemirror-state",
        replacement: resolve(
          __dirname,
          `${PKGS_EDITOR}/node_modules/prosemirror-state`,
        ),
      },
      {
        find: "prosemirror-view",
        replacement: resolve(
          __dirname,
          `${PKGS_EDITOR}/node_modules/prosemirror-view`,
        ),
      },
      {
        find: "prosemirror-model",
        replacement: resolve(
          __dirname,
          `${PKGS_EDITOR}/node_modules/prosemirror-model`,
        ),
      },
      {
        find: "prosemirror-transform",
        replacement: resolve(
          __dirname,
          `${PKGS_EDITOR}/node_modules/prosemirror-transform`,
        ),
      },
      {
        find: "prosemirror-gapcursor",
        replacement: resolve(
          __dirname,
          `${PKGS_EDITOR}/node_modules/prosemirror-gapcursor`,
        ),
      },
      {
        find: "prosemirror-keymap",
        replacement: resolve(
          __dirname,
          `${PKGS_EDITOR}/node_modules/prosemirror-keymap`,
        ),
      },
      {
        find: "~",
        replacement: resolve(__dirname, "src"),
      },
    ],
  },
  server: {
    sourcemapIgnoreList: (sourcePath) => sourcePath.includes("node_modules"),
  },
})
