import js from '@eslint/js';
import tseslint from '@typescript-eslint/eslint-plugin';
import tsparser from '@typescript-eslint/parser';

export default [
  {
    ignores: [
      'dist/**',
      'node_modules/**',
      'coverage/**',
      '*.config.js',
      '*.config.ts',
      '**/dist/**',
      '__LOCAL_LLMs/**',
      // (re-trigger CI: 2026-05-23)

      // Python virtual env in extraction-service/python ships with
      // urllib3's emscripten worker stubs (JS files that reference
      // browser globals like `self`, `fetch`, `TextEncoder`). Those
      // files are NOT project source and must never be linted.
      '**/.venv/**',
      '**/venv/**',
      '**/__pycache__/**',
      // Playwright artifacts (per-package configs already exclude
      // these, but the root config is what runs in CI).
      '**/playwright-report/**',
      '**/test-results/**',
      // .pnpmfile.cjs / .cjs config files use CommonJS require() by
      // design and must not be linted with the TypeScript ESM rule set
      // that forbids require-style imports.
      '**/.pnpmfile.cjs',
      '**/*.cjs',
    ],
  },
  js.configs.recommended,
  {
    files: ['**/*.ts', '**/*.tsx'],
    languageOptions: {
      parser: tsparser,
      parserOptions: {
        ecmaVersion: 2022,
        sourceType: 'module',
        // project: true, // Disabled for now
      },
      globals: {
        process: 'readonly',
        Buffer: 'readonly',
        __dirname: 'readonly',
        __filename: 'readonly',
        Headers: 'readonly',
        TextEncoder: 'readonly',
        TextDecoder: 'readonly',
        RequestInit: 'readonly',
        HeadersInit: 'readonly',
        Response: 'readonly',
        Request: 'readonly',
        fetch: 'readonly',
        console: 'readonly',
        performance: 'readonly',
        AbortSignal: 'readonly',
        AbortController: 'readonly',
        setTimeout: 'readonly',
        clearTimeout: 'readonly',
        setInterval: 'readonly',
        clearInterval: 'readonly',
        navigator: 'readonly',
        document: 'readonly',
        window: 'readonly',
        expect: 'readonly',
        describe: 'readonly',
        it: 'readonly',
        test: 'readonly',
        beforeEach: 'readonly',
        afterEach: 'readonly',
        beforeAll: 'readonly',
        afterAll: 'readonly',
        crypto: 'readonly',
        CryptoKey: 'readonly',
        SubtleCrypto: 'readonly',
        AesGcmParams: 'readonly',
        Crypto: 'readonly',
        ArrayBufferView: 'readonly',
        indexedDB: 'readonly',
        IDBDatabase: 'readonly',
        IDBRequest: 'readonly',
        IDBTransaction: 'readonly',
        IDBObjectStore: 'readonly',
        Blob: 'readonly',
        File: 'readonly',
        FormData: 'readonly',
        URLSearchParams: 'readonly',
        URL: 'readonly',
        BodyInit: 'readonly',
        ReadableStream: 'readonly',
        localStorage: 'readonly',
        sessionStorage: 'readonly',
        ReactNode: 'readonly',
        React: 'readonly',
        JSX: 'readonly',
        HTMLElement: 'readonly',
        HTMLButtonElement: 'readonly',
        HTMLSpanElement: 'readonly',
        HTMLDivElement: 'readonly',
        HTMLInputElement: 'readonly',
        HTMLFormElement: 'readonly',
        HTMLAnchorElement: 'readonly',
        HTMLImageElement: 'readonly',
        HTMLTextAreaElement: 'readonly',
        HTMLSelectElement: 'readonly',
        HTMLLabelElement: 'readonly',
        Element: 'readonly',
        Event: 'readonly',
        KeyboardEvent: 'readonly',
        KeyboardEventInit: 'readonly',
        MouseEvent: 'readonly',
        FocusEvent: 'readonly',
        CustomEvent: 'readonly',
        StorageEvent: 'readonly',
        MediaQueryList: 'readonly',
        MutationObserver: 'readonly',
        ResizeObserver: 'readonly',
        IntersectionObserver: 'readonly',
        EventTarget: 'readonly',
        NodeList: 'readonly',
        DOMRect: 'readonly',
        SVGElement: 'readonly',
        XMLHttpRequest: 'readonly',
        ProgressEvent: 'readonly',
      },
    },
    plugins: {
      '@typescript-eslint': tseslint,
    },
    rules: {
      // TypeScript specific rules
      '@typescript-eslint/no-unused-vars': [
        'error',
        {
          argsIgnorePattern: '^_',
          varsIgnorePattern: '^_',
          caughtErrorsIgnorePattern: '^_',
          destructuredArrayIgnorePattern: '^_',
        },
      ],
      '@typescript-eslint/explicit-function-return-type': 'off',
      '@typescript-eslint/explicit-module-boundary-types': 'off',
      '@typescript-eslint/no-explicit-any': 'warn',

      // General rules
      'no-console': 'warn',
      'no-debugger': 'error',
      'prefer-const': 'error',
      'no-var': 'error',
      'no-unused-vars': 'off', // Use TypeScript version

      // Import rules - disabled for now
      // 'sort-imports': ['error', { ignoreDeclarationSort: true }],
    },
  },
  // Node CJS scripts (build helpers, validators) and `**/scripts/**/*.js` —
  // these run on Node directly and need the Node globals (process, console,
  // require, module, __dirname). Without this block they trip 40+ no-undef
  // errors and break `pnpm lint` for the whole workspace.
  {
    files: ['**/*.cjs', '**/scripts/**/*.js', '**/scripts/**/*.cjs'],
    languageOptions: {
      ecmaVersion: 2022,
      sourceType: 'commonjs',
      globals: {
        process: 'readonly',
        Buffer: 'readonly',
        console: 'readonly',
        require: 'readonly',
        module: 'readonly',
        exports: 'readonly',
        __dirname: 'readonly',
        __filename: 'readonly',
        setTimeout: 'readonly',
        clearTimeout: 'readonly',
        setInterval: 'readonly',
        clearInterval: 'readonly',
      },
    },
    rules: {
      'no-console': 'off', // CLI scripts legitimately print to stdout
    },
  },
];
