/**
 * ESLint configuration for Vermine2047 FoundryVTT system
 * Compatible with FoundryVTT v11-v14
 */
module.exports = {
  root: true,
  env: {
    browser: true,
    es2022: true,
    node: false
  },
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended'
  ],
  parserOptions: {
    ecmaVersion: 2022,
    sourceType: 'module',
    ecmaFeatures: {
      impliedStrict: true
    }
  },
  globals: {
    // FoundryVTT global objects
    game: 'readonly',
    ui: 'readonly',
    Hooks: 'readonly',
    CONFIG: 'readonly',
    Canvas: 'readonly',
    ChatMessage: 'readonly',
    Roll: 'readonly',
    Actor: 'readonly',
    Item: 'readonly',
    Dialog: 'readonly',
    foundry: 'readonly',
    Handlebars: 'readonly',
    renderTemplate: 'readonly',
    fromUuid: 'readonly'
  },
  rules: {
    // Possible Problems
    'no-console': 'error',
    'no-constant-condition': 'error',
    'no-control-regex': 'error',
    'no-debugger': 'error',
    'no-dupe-args': 'error',
    'no-dupe-keys': 'error',
    'no-duplicate-case': 'error',
    'no-empty': 'warn',
    'no-empty-character-class': 'error',
    'no-ex-assign': 'error',
    'no-extra-boolean-cast': 'error',
    'no-fallthrough': 'error',
    'no-func-assign': 'error',
    'no-inner-html': 'off', // Foundry uses innerHTML extensively
    'no-invalid-regexp': 'error',
    'no-irregular-whitespace': 'error',
    'no-misleading-character-class': 'error',
    'no-new-symbol': 'error',
    'no-obj-calls': 'error',
    'no-octal': 'error',
    'no-prototype-builtins': 'error',
    'no-regex-spaces': 'error',
    'no-self-assign': 'error',
    'no-sparse-arrays': 'error',
    'no-template-curly-in-string': 'warn',
    'no-unexpected-multiline': 'error',
    'no-unreachable': 'error',
    'no-unsafe-finally': 'error',
    'no-unsafe-negation': 'error',
    'no-unsafe-optional-chaining': 'error',
    'no-useless-backreference': 'error',
    'require-atomic-updates': 'off',
    'use-isnan': 'error',
    'valid-typeof': 'error',

    // Suggestions
    'accessor-pairs': 'warn',
    'arrow-body-style': ['warn', 'as-needed'],
    'block-scoped-var': 'error',
    'camelcase': ['warn', { allow: ['^_', '^VERMINE_'] }],
    'class-methods-use-this': 'off', // Many utility methods don't use this
    'complexity': ['warn', 20],
    'consistent-return': 'warn',
    'consistent-this': 'warn',
    'curly': ['warn', 'multi-line', 'consistent'],
    'default-case': 'warn',
    'default-case-last': 'warn',
    'default-param-last': 'warn',
    'dot-notation': ['warn', { allowKeywords: true }],
    'eqeqeq': ['error', 'always', { null: 'ignore' }],
    'func-name-matching': 'warn',
    'func-names': ['warn', 'as-needed'],
    'func-style': ['warn', 'declaration', { allowArrowFunctions: true }],
    'grouped-accessor-pairs': 'warn',
    'guard-for-in': 'warn',
    'id-blacklist': 'off',
    'id-length': 'off',
    'id-match': 'off',
    'init-declarations': ['warn', 'always'],
    'line-comment-position': 'off',
    'lines-between-class-members': ['warn', 'always', { exceptAfterSingleLine: true }],
    'logical-assignment-operators': ['warn', 'always'],
    'max-classes-per-file': ['warn', 1],
    'max-depth': ['warn', 5],
    'max-lines': ['warn', { max: 500, skipBlankLines: true, skipComments: true }],
    'max-lines-per-function': ['warn', { max: 100, skipBlankLines: true, skipComments: true, IIFEs: true }],
    'max-nested-callbacks': ['warn', 3],
    'max-params': ['warn', 5],
    'max-statements': ['warn', 30],
    'multiline-comment-style': 'off',
    'new-cap': ['warn', { newIsCap: true, capIsNew: false }],
    'no-alert': 'warn',
    'no-array-constructor': 'error',
    'no-bitwise': 'warn',
    'no-caller': 'error',
    'no-case-declarations': 'error',
    'no-class-assign': 'error',
    'no-cond-assign': ['error', 'always'],
    'no-confusing-arrow': ['warn', { allowParens: true }],
    'no-const-assign': 'error',
    'no-continue': 'off',
    'no-delete-var': 'error',
    'no-div-regex': 'warn',
    'no-else-return': ['warn', { allowElseIf: true }],
    'no-empty-function': ['warn', { allow: ['constructors'] }],
    'no-empty-pattern': 'warn',
    'no-eq-null': 'off',
    'no-eval': 'error',
    'no-extend-native': 'error',
    'no-extra-bind': 'warn',
    'no-extra-label': 'warn',
    'no-floating-decimal': 'warn',
    'no-global-assign': 'error',
    'no-implicit-coercion': ['warn', { allow: ['!!', '+'] }],
    'no-implicit-globals': 'error',
    'no-implied-eval': 'error',
    'no-inline-comments': 'off',
    'no-invalid-this': 'off',
    'no-iterator': 'warn',
    'no-label-var': 'error',
    'no-labels': ['warn', { allowLoop: true, allowSwitch: true }],
    'no-lone-blocks': 'warn',
    'no-lonely-if': 'warn',
    'no-loop-func': 'warn',
    '@typescript-eslint/no-magic-numbers': 'off',
    'no-multi-assign': 'warn',
    'no-multi-str': 'warn',
    'no-negated-condition': 'warn',
    'no-nested-ternary': 'warn',
    'no-new': 'warn',
    'no-new-func': 'warn',
    'no-new-wrappers': 'error',
    'no-nonoctal-decimal-escape': 'error',
    'no-octal-escape': 'error',
    'no-param-reassign': ['warn', { props: false }],
    'no-plusplus': 'off',
    'no-promise-executor-return': 'error',
    'no-proto': 'error',
    'no-redeclare': 'error',
    'no-regex-spaces': 'error',
    'no-restricted-globals': 'off',
    'no-restricted-imports': 'off',
    'no-restricted-modules': 'off',
    'no-restricted-properties': 'off',
    'no-restricted-syntax': 'off',
    'no-return-assign': ['error', 'always'],
    'no-return-await': 'error',
    'no-script-url': 'warn',
    'no-sequences': 'error',
    'no-setter-return': 'error',
    'no-shadow': 'warn',
    'no-shadow-restricted-names': 'error',
    'no-sparse-arrays': 'error',
    'no-tabs': 'warn',
    'no-template-curly-in-string': 'warn',
    'no-ternary': 'off',
    'no-this-before-super': 'error',
    'no-throw-literal': 'warn',
    'no-undef': ['error', { typeof: true }],
    'no-undef-init': 'warn',
    'no-undefined': 'off',
    'no-underscore-dangle': ['warn', { allow: ['_id', '_on', '_source', '_total', '_html'] }],
    'no-unneeded-ternary': ['warn', { defaultAssignment: false }],
    'no-unreachable-loop': 'warn',
    'no-unsafe-finally': 'error',
    'no-unsafe-negation': 'error',
    'no-unused-expressions': ['warn', { allowShortCircuit: true, allowTernary: true, enforceForJSX: false }],
    'no-unused-labels': 'warn',
    'no-unused-private-class-members': 'warn',
    'no-unused-vars': ['warn', { args: 'none', caughtErrors: 'none', ignoreRestSiblings: true }],
    'no-use-before-define': ['warn', { functions: false, classes: false, variables: true }],
    'no-useless-call': 'warn',
    'no-useless-catch': 'warn',
    'no-useless-computed-key': ['warn', { enforceForClassMembers: false }],
    'no-useless-concat': 'warn',
    'no-useless-constructor': 'warn',
    'no-useless-escape': 'warn',
    'no-useless-rename': 'warn',
    'no-useless-return': 'warn',
    'no-var': 'error',
    'no-void': ['warn', { allowAsStatement: true }],
    'no-warning-comments': 'warn',
    'no-with': 'error',
    'object-shorthand': ['warn', 'always', { ignoreConstructors: false, avoidQuotes: true }],
    'one-var': ['warn', 'never'],
    'one-var-declaration-per-line': ['warn', 'initializations'],
    'operator-assignment': ['warn', 'always'],
    'prefer-arrow-callback': 'warn',
    'prefer-const': ['error', { destructuring: 'all', ignoreReadBeforeAssign: false }],
    'prefer-destructuring': ['warn', { array: false, object: true }],
    'prefer-exponentiation-operator': 'warn',
    'prefer-named-capture-group': 'off',
    'prefer-numeric-literals': 'warn',
    'prefer-object-has-own': 'warn',
    'prefer-object-spread': 'warn',
    'prefer-promise-reject-errors': ['error', { allowEmptyReject: false }],
    'prefer-regex-literals': ['warn', { disallowRedundantWrapping: true }],
    'prefer-rest-params': 'warn',
    'prefer-spread': 'warn',
    'prefer-template': 'warn',
    'quote-props': ['warn', 'as-needed'],
    'radix': ['error', 'always'],
    'require-await': 'warn',
    'require-unicode-regexp': 'off',
    'require-yield': 'error',
    'sort-imports': 'off',
    'sort-keys': 'off',
    'sort-vars': 'off',
    'spaced-comment': ['warn', 'always', { line: { markers: ['!', '/'] }, block: { balanced: true, markers: ['!', '*'], exceptions: ['*'] } }],
    'strict': ['error', 'never'],
    'symbol-description': 'warn',
    'unicode-bom': ['error', 'never'],
    'vars-on-top': 'off',
    'yoda': ['warn', 'never', { exceptRange: true }],

    // Layout & Formatting
    'array-bracket-newline': ['warn', 'consistent'],
    'array-bracket-spacing': ['warn', 'never'],
    'array-element-newline': ['warn', 'consistent'],
    'arrow-parens': ['warn', 'always'],
    'arrow-spacing': ['warn', { before: true, after: true }],
    'block-spacing': ['warn', 'always'],
    'brace-style': ['warn', '1tbs', { allowSingleLine: true }],
    'comma-dangle': ['warn', {
      arrays: 'always-multiline',
      objects: 'always-multiline',
      imports: 'always-multiline',
      exports: 'always-multiline',
      functions: 'always-multiline'
    }],
    'comma-spacing': ['warn', { before: false, after: true }],
    'comma-style': ['warn', 'last', { exceptions: { VariableDeclarator: true, ArrayExpression: true, ObjectExpression: true } }],
    'computed-property-spacing': ['warn', 'never', { enforceForClassMembers: true }],
    'dot-notation': ['warn', { allowKeywords: true }],
    'eol-last': ['warn', 'always'],
    'func-call-spacing': ['warn', 'never'],
    'func-style': ['warn', 'declaration', { allowArrowFunctions: true }],
    'function-call-argument-newline': ['warn', 'consistent'],
    'function-paren-newline': ['warn', 'consistent'],
    'generator-star-spacing': ['warn', { before: false, after: true }],
    'implicit-arrow-linebreak': ['warn', 'beside'],
    'indent': ['warn', 2, {
      SwitchCase: 1,
      VariableDeclarator: { var: 2, let: 2, const: 3 },
      outerIIFEBody: 1,
      MemberExpression: 'off',
      FunctionDeclaration: { body: 1, parameters: 1 },
      FunctionExpression: { body: 1, parameters: 1 },
      StaticBlock: { body: 1 }
    }],
    'jsx-quotes': 'off',
    'key-spacing': ['warn', { beforeColon: false, afterColon: true, mode: 'strict' }],
    'keyword-spacing': ['warn', { before: true, after: true, overrides: { return: { after: true }, throw: { after: true }, case: { after: true } } }],
    'line-comment-position': 'off',
    'linebreak-style': ['warn', 'unix'],
    'lines-around-comment': 'off',
    'lines-between-class-members': ['warn', 'always', { exceptAfterSingleLine: true }],
    'max-len': ['warn', {
      code: 120,
      tabWidth: 2,
      comments: 120,
      ignoreComments: false,
      ignoreTrailingComments: true,
      ignoreUrls: true,
      ignoreStrings: true,
      ignoreTemplateLiterals: true,
      ignoreRegExpLiterals: true
    }],
    'max-statements-per-line': ['warn', { max: 1 }],
    'multiline-comment-style': 'off',
    'multiline-ternary': ['warn', 'always-multiline'],
    'new-parens': 'warn',
    'newline-per-chained-call': ['warn', { ignoreChainWithDepth: 3 }],
    'no-extra-parens': ['warn', 'all', { conditionalAssign: false, returnAssign: false, nestedBinaryExpressions: false, ignoreJSX: 'all', enforceForArrowConditionals: false, enforceForSequenceExpressions: false, enforceForNewInMemberExpressions: false }],
    'no-extra-semi': 'warn',
    'no-floating-decimal': 'warn',
    'no-mixed-operators': ['warn', { groups: [['+', '-', '*', '/', '%', '**'], ['&', '|', '^', '~', '<<', '>>', '>>>'], ['==', '!=', '===', '!==', '>', '>=', '<', '<='], ['&&', '||'], ['in', 'instanceof']], allowSamePrecedence: false }],
    'no-mixed-spaces-and-tabs': 'warn',
    'no-multi-spaces': ['warn', { ignoreEOLComments: false, exceptions: { Property: true, BinaryExpression: false, VariableDeclarator: true, ImportDeclaration: true } }],
    'no-multiple-empty-lines': ['warn', { max: 1, maxEOF: 0, maxBOF: 0 }],
    'no-tabs': 'warn',
    'no-trailing-spaces': ['warn', { skipBlankLines: false, ignoreComments: false }],
    'no-whitespace-before-property': 'warn',
    'nonblock-statement-body-position': ['warn', 'beside', { overrides: { if: 'beside', while: 'beside', do: 'beside', for: 'beside' } }],
    'object-curly-newline': ['warn', { multiline: true, consistent: true }],
    'object-curly-spacing': ['warn', 'always'],
    'object-property-newline': ['warn', { allowAllPropertiesOnSameLine: true }],
    'operator-linebreak': ['warn', 'after', { overrides: { '?': 'before', ':': 'before', '||': 'after', '&&': 'after', '|>': 'after' } }],
    'padded-blocks': ['warn', 'never'],
    'padding-line-between-statements': 'off',
    'prefer-exponentiation-operator': 'warn',
    'quote-props': ['warn', 'as-needed'],
    'quotes': ['warn', 'single', { avoidEscape: true, allowTemplateLiterals: true }],
    'rest-spread-spacing': ['warn', 'never'],
    'semi': ['warn', 'always'],
    'semi-spacing': ['warn', { before: false, after: true }],
    'semi-style': ['warn', 'last'],
    'space-before-blocks': ['warn', 'always'],
    'space-before-function-paren': ['warn', { anonymous: 'always', named: 'never', asyncArrow: 'always' }],
    'space-in-parens': ['warn', 'never'],
    'space-infix-ops': 'warn',
    'space-unary-ops': ['warn', { words: true, nonwords: false, overrides: {} }],
    'switch-colon-spacing': ['warn', { after: true, before: false }],
    'template-curly-spacing': 'warn',
    'template-tag-spacing': ['warn', 'never'],
    'unicode-bom': ['error', 'never'],
    'wrap-iife': ['warn', 'outside', { functionPrototypeMethods: true }],
    'wrap-regex': 'warn',
    'yield-star-spacing': ['warn', { before: false, after: true }]
  },
  overrides: [
    {
      files: ['*.hbs', '*.handlebars'],
      rules: {
        // Handlebars templates don't need linting
        'no-undef': 'off'
      }
    },
    {
      files: ['**/tests/**', '**/*.test.js', '**/*.spec.js'],
      env: {
        jest: true,
        mocha: true
      }
    }
  ],
  ignorePatterns: [
    'node_modules/',
    'dist/',
    'build/',
    '*.min.js',
    '*.min.css'
  ]
};
