// Workaround for @nuxt/ui v4 import protection issue
// This is a temporary fix until @nuxt/ui properly handles ssr: false

import type { Plugin } from 'vite'

export function disableImportProtection(): Plugin {
  return {
    name: 'disable-import-protection-for-nuxt-ui',
    enforce: 'pre',
    resolveId(id, importer) {
      // External node binary files
      if (id.endsWith('.node')) {
        return { id, external: true }
      }

      // External Node.js built-in modules
      const nodeModules = ['fs', 'path', 'url', 'fs/promises', 'node:url', 'node:module', 'node:fs', 'node:path']
      if (nodeModules.includes(id) || id.startsWith('node:')) {
        return { id, external: true }
      }

      // External server-only packages from @nuxt/ui imports
      const serverPackages = ['@nuxt/kit', '@tailwindcss/node', '@tailwindcss/oxide', 'jiti']
      for (const pkg of serverPackages) {
        if (id === pkg || id.startsWith(pkg + '/')) {
          return { id, external: true }
        }
      }
    }
  }
}
