import { defineConfig } from 'vitest/config';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import path from 'path';

export default defineConfig({
  plugins: [
    svelte()
  ],
  test: {
    globals: true,
    environment: 'jsdom',
    include: [
      'src/**/*.{test,spec}.{js,ts}',
      'src/lib/**/*.test.{js,ts}',
      'src/lib/**/__tests__/*.{test,spec}.{js,ts}',
      'src/lib/**/__tests__/test_*.{js,ts}',
      'tests/**/*.test.{js,ts}'
    ],
    exclude: [
      'node_modules/**',
      'dist/**'
    ],
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html'],
      include: [
        'src/lib/stores/**/*.{js,ts}',
        'src/lib/models/**/*.{js,ts}',
        'src/lib/api/**/*.{js,ts}',
        'src/lib/auth/**/*.{js,ts}',
        'src/lib/helpers/**/*.{js,ts}',
        'src/lib/utils/**/*.{js,ts}',
        'src/lib/api.ts',
        'src/lib/routes.ts',
        'src/lib/utils.ts',
        'src/lib/cot-logger.ts',
        'src/lib/toasts.svelte.ts',
        'src/lib/stores.svelte.ts',
        'src/lib/ui/**/*.{js,ts,svelte}',
        '!src/lib/**/*.test.*',
        '!src/lib/**/__tests__/**',
        '!src/lib/**/mocks/**',
        '!src/lib/**/*.spec.*',
        '!src/lib/**/locales/**',
      ],
      exclude: [
        'src/lib/components/**',
        'src/lib/i18n/**',
        'src/lib/Counter.svelte',
        'src/lib/stores/__tests__/setupTests.ts',
        'src/lib/stores/__tests__/mocks/**',
        'src/routes/**',
        'src/services/**',
        'src/components/**',
        'node_modules/**',
        'dist/**',
      ],
      thresholds: {
        perFile: true,
        statements: 98,
        lines: 98,
        functions: 95,
        branches: 80,
      },
      clean: true,
    },
    setupFiles: ['./src/lib/stores/__tests__/setupTests.js'],
    alias: [
      { find: '$app/environment', replacement: path.resolve(__dirname, './src/lib/stores/__tests__/mocks/environment.js') },
      { find: '$app/stores', replacement: path.resolve(__dirname, './src/lib/stores/__tests__/mocks/stores.js') },
      { find: '$app/state', replacement: path.resolve(__dirname, './src/lib/stores/__tests__/mocks/state.js') },
      { find: '$app/navigation', replacement: path.resolve(__dirname, './src/lib/stores/__tests__/mocks/navigation.js') },
      { find: '$env/static/public', replacement: path.resolve(__dirname, './src/lib/stores/__tests__/mocks/env_public.js') },
      { find: '$components', replacement: path.resolve(__dirname, './src/components') }
    ]
  },
  resolve: {
    conditions: ['mode=browser', 'browser'],
    alias: {
      '$lib': path.resolve(__dirname, './src/lib'),
      '$app': path.resolve(__dirname, './src'),
      '$components': path.resolve(__dirname, './src/components')
    }
  }
});