/// <reference types="vite/client" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { resolve } from 'path';

/**
 * Root-level Vite library-mode config for the <kerf-embed> Web Component.
 *
 * Mirrors app/vite.embed.config.ts but resolves paths relative to the
 * monorepo root so `npx vite build --config vite.embed.config.ts` works
 * from the project root directory.
 */
export default defineConfig({
  root: resolve(__dirname, 'app'),
  plugins: [react()],
  build: {
    outDir: resolve(__dirname, 'dist-embed'),
    emptyOutDir: true,
    lib: {
      entry: resolve(__dirname, 'app/src/embed.tsx'),
      name: 'KerfEmbed',
      formats: ['es', 'iife'],
      fileName: (format) => {
        if (format === 'es') return 'kerf-embed.js';
        return 'kerf-embed.iife.js';
      },
    },
    cssCodeSplit: false,
    rollupOptions: {
      external: [],
      output: {
        codeSplitting: false,
        assetFileNames: (assetInfo) => {
          if (assetInfo.names?.[0]?.endsWith('.css')) return 'style.css';
          return assetInfo.names?.[0] ?? '[name][extname]';
        },
      },
    },
  },
});
