import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';

export default defineConfig({
  root: __dirname,
  // Env lives at the workspace root, shared with the portal — without this
  // Vite looks in apps/backoffice and VITE_BASE_API_URL silently falls back to
  // its built-in default.
  envDir: '../../',
  cacheDir: '../../node_modules/.vite/apps/backoffice',
  server: {
    port: 4201,
    host: 'localhost',
  },
//   server: {
//   port: 4201,
//   proxy: {
//     '/api': {
//       target: 'http://localhost:3000', // change from 'https://ema-api-dev.triaplc.com'
//       changeOrigin: true,
//     },
//   },
// },
  preview: { port: 4201, host: 'localhost' },
  plugins: [react(), nxViteTsPaths()],
  resolve: {
    dedupe: ['react', 'react-dom', 'react-router-dom', '@tanstack/react-query'],
    alias: {
      // The vendored user-management UI pulls in file-type → token-types,
      // which imports `node:buffer`. Point it at the browser polyfill so the
      // production build resolves it instead of failing on a Node built-in.
      'node:buffer': 'buffer',
    },
  },
  define: {
    // `buffer` expects a `global` binding that browsers do not provide.
    global: 'globalThis',
  },
  build: {
    outDir: '../../dist/apps/backoffice',
    emptyOutDir: true,
    reportCompressedSize: true,
  },
  // Unit tests for the pure helpers behind a screen (formatters, URL state).
  // Component tests are deliberately not set up: nothing here renders React,
  // so no jsdom environment or setup file is needed.
  // test: {
  //   watch: false,
  //   globals: true,
  //   environment: 'node',
  //   include: ['src/**/*.spec.ts'],
  //   reporters: ['default'],
  // },
});
