/**
 * npm install --save-dev @typescript-eslint/eslint-plugin @typescript-eslint/parser \
 *   eslin eslint-config-prettier eslint-config-typescript eslint-plugin-import \
 *   eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks \
 *   prettier typescript
 */

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  extends: [
    'eslint:recommended',
    'plugin:import/errors',
    'plugin:react/recommended',
    'plugin:react-hooks/recommended',
    'prettier'
  ],
  plugins: ['import', 'prettier'],
  rules: {
    'prettier/prettier': 'error',
    'max-classes-per-file': 'error',
    'use-isnan': 'error',
    'no-new-wrappers': 'error',
    'no-continue': 'error',
    'no-cond-assign': ['error', 'always'],
    radix: ['error', 'always'],
    curly: 'error',
    eqeqeq: ['error', 'always'],
    'no-restricted-globals': [
      'error',
      { name: 'isNaN', message: 'Use `Number.isNaN` instead.' },
      { name: 'isFinite', message: 'Use `Number.isFinite` instead.' },
      {
        name: 'fdescribe',
        message: 'Do not commit `fdescribe`. Use `describe` instead.'
      },
      { name: 'fit', message: 'Do not commit `fit`. Use `it` instead.' }
    ],
    'no-restricted-properties': [
      'error',
      {
        object: 'arguments',
        property: 'callee',
        message: 'arguments.callee is deprecated'
      },
      {
        property: '__defineGetter__',
        message: 'Please use Object.defineProperty instead.'
      },
      {
        property: '__defineSetter__',
        message: 'Please use Object.defineProperty instead.'
      },
      {
        object: 'describe',
        property: 'only',
        message: 'Do not commit `describe.only`. Use `describe` instead.'
      },
      {
        object: 'it',
        property: 'only',
        message: 'Do not commit `it.only`. Use `it` instead.'
      },
      {
        object: 'test',
        property: 'only',
        message: 'Do not commit `test.only`. Use `test` instead.'
      }
    ],
    'import/order': [
      'error',
      {
        'newlines-between': 'always',
        alphabetize: { order: 'asc', caseInsensitive: true }
      }
    ],
    'import/newline-after-import': 'error',
    'sort-imports': [
      'error',
      {
        ignoreDeclarationSort: true,
        ignoreCase: false,
        ignoreMemberSort: false
      }
    ] // Everything else done by `import` plugin
  },
  overrides: [
    {
      // enable the rule specifically for TypeScript files
      files: ['src/**/*.ts', 'src/**/*.tsx'],
      extends: [
        'plugin:@typescript-eslint/recommended',
        'plugin:@typescript-eslint/recommended-requiring-type-checking',
        'plugin:import/typescript'
      ],
      parser: '@typescript-eslint/parser',
      parserOptions: {
        project: './tsconfig.json',
        ecmaFeatures: {
          jsx: true
        }
      },
      rules: {
        '@typescript-eslint/array-type': [
          'error',
          {
            default: 'array',
            readonly: 'array'
          }
        ],
        '@typescript-eslint/explicit-function-return-type': [
          'error',
          { allowExpressions: true, allowTypedFunctionExpressions: true }
        ],
        '@typescript-eslint/no-unsafe-member-access': 'error',
        '@typescript-eslint/explicit-module-boundary-types': 'error',
        '@typescript-eslint/no-inferrable-types': 'off',
        '@typescript-eslint/no-explicit-any': 'error',
        '@typescript-eslint/no-unused-vars': 'error',
        '@typescript-eslint/no-misused-new': 'error',
        '@typescript-eslint/member-ordering': [
          'error',
          {
            default: [
              // Index signature
              'signature',
              // Fields
              'public-static-field',
              'protected-static-field',
              'private-static-field',
              'public-decorated-field',
              'protected-decorated-field',
              'private-decorated-field',
              'public-instance-field',
              'protected-instance-field',
              'private-instance-field',
              'public-abstract-field',
              'protected-abstract-field',
              'private-abstract-field',
              // Constructors
              'public-constructor',
              'protected-constructor',
              'private-constructor',
              // Methods
              'public-static-method',
              'protected-static-method',
              'private-static-method',
              'public-decorated-method',
              'protected-decorated-method',
              'private-decorated-method',
              'public-instance-method',
              'protected-instance-method',
              'private-instance-method',
              'public-abstract-method',
              'protected-abstract-method',
              'private-abstract-method'
            ]
          }
        ],
        // We must disable the base rule as it can report incorrect errors
        'lines-between-class-members': 'off',
        '@typescript-eslint/lines-between-class-members': [
          'error',
          'always',
          {
            exceptAfterOverload: true,
            exceptAfterSingleLine: true
          }
        ],
        '@typescript-eslint/no-unsafe-assignment': 'off',
        '@typescript-eslint/consistent-type-definitions': [
          'error',
          'interface'
        ],
        '@typescript-eslint/member-delimiter-style': [
          'error',
          {
            multiline: { delimiter: 'semi', requireLast: true },
            singleline: { delimiter: 'semi', requireLast: false }
          }
        ],
        '@typescript-eslint/no-empty-interface': [
          'error',
          {
            allowSingleExtends: true
          }
        ],
        '@typescript-eslint/naming-convention': [
          'error',
          {
            selector: 'default',
            format: ['camelCase'],
            leadingUnderscore: 'allow',
            trailingUnderscore: 'allow'
          },
          {
            selector: 'variable',
            format: ['camelCase', 'UPPER_CASE', 'PascalCase'],
            leadingUnderscore: 'allow',
            trailingUnderscore: 'allow'
          },
          {
            selector: 'function',
            format: ['camelCase', 'PascalCase'],
            leadingUnderscore: 'allow',
            trailingUnderscore: 'allow'
          },
          {
            selector: 'typeLike',
            format: ['PascalCase']
          },
          {
            selector: 'enumMember',
            format: ['PascalCase']
          },
          {
            selector: 'property',
            modifiers: ['static'],
            format: ['UPPER_CASE'],
            leadingUnderscore: 'allow',
            trailingUnderscore: 'allow'
          },
          {
            selector: 'property',
            modifiers: ['static'],
            types: ['function'],
            format: ['camelCase'],
            leadingUnderscore: 'allow',
            trailingUnderscore: 'allow'
          }
        ]
      }
    }
  ]
};
