import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import Components from 'unplugin-vue-components/vite'
import { PrimeVueResolver } from '@primevue/auto-import-resolver'
import AutoImport from 'unplugin-auto-import/vite'
import { unheadComposablesImports } from 'unhead'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  console.log('Building in mode', mode)
  return {
    build: {
      sourcemap: mode === 'development',
      reportCompressedSize: false,

      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              // PouchDB и его плагины
              if (id.includes('pouchdb')) {
                return 'vendor_pouchdb'
              }

              // QR-коды и библиотеки для работы с ними
              if (id.includes('qr-code-styling')) {
                return 'vendor_qr-code-styling'
              }

              // PrimeVue и связанные модули
              if (id.includes('primevue')) {
                return 'vendor_primevue'
              }

              // SVGO и зависимости
              if (id.includes('svgo') || id.includes('@trysound')) {
                return 'vendor_svgo'
              }

              // Танстэк Vue Query и ядро
              if (id.includes('@tanstack')) {
                return 'vendor_tanstack-query'
              }

              // Vue и связанные библиотеки
              if (id.includes('vue')) {
                return 'vendor_vue'
              }

              if (id.includes('css-tree')) {
                return 'vendor_css-tree'
              }

              if (id.includes('css-what')) {
                return 'vendor_css-what'
              }

              if (id.includes('domutils')) {
                return 'vendor_domutils'
              }

              if (id.includes('source-map-js')) {
                return 'vendor_source-map-js'
              }

              if (id.includes('csso')) {
                return 'vendor_csso'
              }
              if (id.includes('entities')) {
                return 'vendor_entities'
              }

              // Остальные модули из node_modules в vendor
              return 'vendor'
            }

            if (id.includes('src/api/')) {
              return 'api'
            }
            if (id.includes('src/utils/')) {
              return 'utils'
            }

            // По умолчанию ничего не делим
            return null
          },
        },
      },
    },
    plugins: [
      vue(),
      AutoImport({
        imports: ['vue', 'vue-router', unheadComposablesImports[0]],
      }),
      Components({
        resolvers: [PrimeVueResolver()],
      }),
      vueDevTools(),
    ],
    define: {
      global: 'window', // it is needed for PouchDB
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
  }
})
