import { createRequire } from 'node:module'
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
import eslintPlugin from 'vite-plugin-eslint'
import svgr from 'vite-plugin-svgr'
import path from 'path'
import { fileURLToPath } from 'url'
import { visualizer } from 'rollup-plugin-visualizer'

const require = createRequire(import.meta.url)
const svgo = require('vite-plugin-svgo')

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const isCloudflare = process.env.VITE_DEPLOY_TARGET === 'cloudflare'

export default defineConfig({
  base: isCloudflare ? '/' : './', // Cloudflare Pages needs absolute paths; Electron needs relative for file://
  resolve: {
    alias: {
      three: path.resolve(__dirname, 'node_modules/three')
    },
    dedupe: [
      '@codemirror/state',
      '@codemirror/view',
      '@codemirror/language',
      '@codemirror/commands',
      'codemirror'
    ]
  },
  optimizeDeps: {
    include: [
      '@ant-design/icons',
      '@codemirror/state',
      '@codemirror/view',
      '@codemirror/language',
      'codemirror',
      '@uiw/react-codemirror'
    ]
  },
  plugins: [
    react(),
    svgo(),
    svgr(),
    eslintPlugin(),
    visualizer(),
    // Transform index.html paths to absolute when building for Cloudflare Pages
    isCloudflare && {
      name: 'cloudflare-html-paths',
      transformIndexHtml(html) {
        return html
          .replace(/href="\.\//g, 'href="/')
          .replace(/src="\.\//g, 'src="/')
      }
    }
  ].filter(Boolean),
  build: {
    outDir: 'build',
    chunkSizeWarningLimit: 2000,
    rollupOptions: {
      output: {
        manualChunks(id) {
          // --- CodeMirror
          if (id.includes('node_modules/@codemirror')) {
            return 'codemirror'
          }

          // --- Lezer
          if (id.includes('node_modules/@lezer')) {
            return 'lezer'
          }

          // --- Core Vendor (Ant Design, React, etc.)
          if (
            id.includes('node_modules/antd') ||
            id.includes('node_modules/@ant-design') ||
            id.includes('node_modules/rc-') ||
            id.includes('node_modules/antd-style') ||
            id.includes('node_modules/@rc-component') ||
            id.includes('node_modules/dayjs') ||
            id.includes('node_modules/react') ||
            id.includes('node_modules/react-dom') ||
            id.includes('node_modules/react-router-dom')
          ) {
            return 'antd'
          }

          // --- Lodash
          if (id.includes('node_modules/lodash')) {
            return 'lodash'
          }

          // --- Three.js
          if (id.includes('node_modules/three')) {
            return 'three'
          }

          // --- GCode Preview
          if (id.includes('node_modules/gcode-preview')) {
            return 'gcode-preview'
          }

          // --- Online 3D Viewer
          if (id.includes('node_modules/online-3d-viewer')) {
            return 'online-3d-viewer'
          }

          // --- tsparticles
          if (
            id.includes('node_modules/@tsparticles') ||
            id.includes('node_modules/tsparticles')
          ) {
            return 'tsparticles'
          }

          // --- Tiptap
          if (id.includes('node_modules/@tiptap')) {
            return 'tiptap'
          }
        }
      }
    }
  },
  server: {
    allowedHosts: ['dev.tombutcher.work'],
    host: '0.0.0.0',
    port: 5173,
    open: false
  }
})
