import js from '@eslint/js'
import tseslint from 'typescript-eslint'
import globals from 'globals'
import prettier from 'eslint-config-prettier'

// Flat config (ESLint 9 + typescript-eslint 8). Codifies the existing house style
// (CC2): typescript-eslint's recommended logic/type rules, with formatting left
// entirely to Prettier (eslint-config-prettier turns off any stylistic rules).
// Run with `npm run lint`; `npm run format` applies Prettier.
export default tseslint.config(
  { ignores: ['out/**', 'dist/**', 'node_modules/**', '**/*.cjs'] },
  js.configs.recommended,
  ...tseslint.configs.recommended,
  {
    languageOptions: {
      globals: { ...globals.node, ...globals.browser }
    },
    rules: {
      // Empty catch blocks are a deliberate best-effort pattern across the app.
      'no-empty': ['error', { allowEmptyCatch: true }],
      // Allow a documented @ts-ignore. The preload's window fallback needs one
      // whose necessity is incremental-build-state-dependent, so @ts-expect-error
      // (which errors when momentarily unused) is the wrong tool there.
      '@typescript-eslint/ban-ts-comment': [
        'error',
        { 'ts-ignore': 'allow-with-description', minimumDescriptionLength: 3 }
      ],
      // Allow intentionally-unused names prefixed with `_` (e.g. IPC event args).
      '@typescript-eslint/no-unused-vars': [
        'error',
        { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }
      ]
    }
  },
  {
    // TypeScript already resolves identifiers/types, so core no-undef only
    // false-positives on globals and type-only references here.
    files: ['**/*.{ts,tsx}'],
    rules: { 'no-undef': 'off' }
  },
  // Keep Prettier last so it wins over any formatting-related rule.
  prettier
)
