import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

const rootDir = dirname(fileURLToPath(import.meta.url))

export default defineConfig({
  plugins: [vue()],
  optimizeDeps: {
    include: [
      'ezuikit-js',
      '@skax/delegate-original/dist/delegate.js',
      'deepmerge',
      'screenfull',
      'debounce-promise',
      '@ezuikit/utils-tools',
      '@ezuikit/utils-i18n',
      '@ezuikit/utils-logger',
      '@ezuikit/utils-collect'
    ],
    needsInterop: [
      'deepmerge',
      'screenfull',
      'debounce-promise',
      '@ezuikit/utils-i18n',
      '@ezuikit/utils-logger',
      '@ezuikit/utils-collect'
    ]
  },
  build: {
    commonjsOptions: {
      include: [/ezuikit-js/, /node_modules/]
    }
  },
  server: {
    port: 3000,
    open: true,
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
      '/profile': {
        target: 'http://localhost:8080',
        changeOrigin: true
      }
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler',
        additionalData: `@use "@/styles/variables.scss" as *;`
      }
    }
  },
  resolve: {
    alias: {
      '@': resolve(rootDir, 'src'),
      '@skax/delegate': resolve(rootDir, 'src/shims/skax-delegate.js'),
      '@skax/delegate-original': resolve(rootDir, 'node_modules/@skax/delegate')
    }
  }
})
