import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
import { defineConfig, loadEnv } from 'vite'
// import mkcert from 'vite-plugin-mkcert'
import solid from 'vite-plugin-solid'

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

// https://vitejs.dev/config/
// production
export default defineConfig(({ mode }) => {
  // eslint-disable-next-line no-undef
  const env = loadEnv(mode, process.cwd(), '')

  if (env.NODE_ENV === 'production') {
    return {
      resolve: {
        alias: {
          '@': path.resolve(_dirname, './src'),
          '@lang': path.resolve(_dirname, './src/lang'),
          '@api': path.resolve(_dirname, './src/api'),
          '@hooks': path.resolve(_dirname, './src/hooks'),
          '@pages': path.resolve(_dirname, './src/pages'),
          '@components': path.resolve(_dirname, './src/components'),
          '@routes': path.resolve(_dirname, './src/routes'),
          '@utils': path.resolve(_dirname, './src/utils'),
          '@assets': path.resolve(_dirname, './src/assets'),
          '@context': path.resolve(_dirname, './src/context'),
        },
      },
      plugins: [solid()],
      server: {
        https: false,
        host: true,
        port: 5001,
        strictPort: true,
        watch: {
          usePolling: true,
        },
      },
    }
  }

  return {
    resolve: {
      alias: {
        '@': path.resolve(_dirname, './src'),
        '@lang': path.resolve(_dirname, './src/lang'),
        '@api': path.resolve(_dirname, './src/api'),
        '@hooks': path.resolve(_dirname, './src/hooks'),
        '@pages': path.resolve(_dirname, './src/pages'),
        '@components': path.resolve(_dirname, './src/components'),
        '@routes': path.resolve(_dirname, './src/routes'),
        '@utils': path.resolve(_dirname, './src/utils'),
        '@assets': path.resolve(_dirname, './src/assets'),
        '@context': path.resolve(_dirname, './src/context'),
      },
    },
    // plugins: [solid(), mkcert()],
    plugins: [solid()],
    server: {
      https: false,
      host: true,
      port: 5001,
      strictPort: true,
      watch: {
        usePolling: true,
      },
      proxy: {
        '/api': 'http://localhost:9000',
      },
    },
  }
})
