import js from '@eslint/js';
import tseslint from 'typescript-eslint';
import sveltePlugin from 'eslint-plugin-svelte';
import svelteParser from 'svelte-eslint-parser';
import prettier from 'eslint-config-prettier';

export default tseslint.config(
	// Base JavaScript recommended rules
	js.configs.recommended,

	// TypeScript recommended rules
	...tseslint.configs.recommended,

	// Global ignores
	{
		ignores: [
			'.svelte-kit/**',
			'build/**',
			'dist/**',
			'node_modules/**',
			'playwright-report/**',
			'test-results/**',
			'test-results-debug/**',
			'*.config.js',
			'*.config.ts'
		]
	},

	// TypeScript files
	{
		files: ['**/*.ts'],
		languageOptions: {
			parserOptions: {
				sourceType: 'module',
				ecmaVersion: 2020
			},
			globals: {
				window: 'readonly',
				document: 'readonly',
				console: 'readonly',
				process: 'readonly',
				__dirname: 'readonly',
				__filename: 'readonly',
				Promise: 'readonly',
				Set: 'readonly',
				Map: 'readonly'
			}
		},
		rules: {
			'@typescript-eslint/no-unused-vars': [
				'error',
				{
					argsIgnorePattern: '^_',
					varsIgnorePattern: '^_'
				}
			],
			'no-console': ['warn', { allow: ['warn', 'error'] }]
		}
	},

	// Svelte files
	{
		files: ['**/*.svelte'],
		languageOptions: {
			parser: svelteParser,
			parserOptions: {
				parser: tseslint.parser,
				sourceType: 'module',
				ecmaVersion: 2020,
				extraFileExtensions: ['.svelte']
			},
			globals: {
				window: 'readonly',
				document: 'readonly',
				console: 'readonly',
				process: 'readonly',
				Promise: 'readonly',
				Set: 'readonly',
				Map: 'readonly',
				Event: 'readonly',
				SubmitEvent: 'readonly',
				fetch: 'readonly',
				HTMLElement: 'readonly',
				HTMLDivElement: 'readonly',
				HTMLSelectElement: 'readonly',
				setInterval: 'readonly',
				clearInterval: 'readonly',
				URL: 'readonly',
				URLSearchParams: 'readonly',
				HTMLInputElement: 'readonly',
				KeyboardEvent: 'readonly',
				AbortController: 'readonly',
				DOMException: 'readonly',
				setTimeout: 'readonly',
				clearTimeout: 'readonly',
				DragEvent: 'readonly',
				File: 'readonly',
				Blob: 'readonly',
				HTMLButtonElement: 'readonly',
				MouseEvent: 'readonly'
			}
		},
		plugins: {
			svelte: sveltePlugin
		},
		rules: {
			...sveltePlugin.configs.recommended.rules,
			'@typescript-eslint/no-unused-vars': [
				'error',
				{
					argsIgnorePattern: '^_',
					varsIgnorePattern: '^_'
				}
			]
		}
	},

	// Prettier (disable conflicting rules)
	prettier
);
