import nhcarrigan from '@nhcarrigan/eslint-config';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __dirname = dirname(fileURLToPath(import.meta.url));
const nhcarriganArray = Array.isArray(nhcarrigan) ? nhcarrigan : [nhcarrigan];

// Jest globals that should be available in test files
const jestGlobals = {
  afterAll: 'readonly',
  afterEach: 'readonly',
  beforeAll: 'readonly',
  beforeEach: 'readonly',
  describe: 'readonly',
  expect: 'readonly',
  it: 'readonly',
  jest: 'readonly',
  test: 'readonly',
};

// Map the nhcarrigan configs to handle shared-types directory structure
const mappedConfigs = nhcarriganArray.flatMap(config => {
  if (!config.files) {
    return [config];
  }

  const newFiles = config.files
    .map(pattern => {
      if (pattern.startsWith('src/')) {
        return pattern.replace('src/', 'shared-types/src/');
      } else if (pattern.startsWith('test/')) {
        return pattern.replace('test/', 'shared-types/test/');
      }
      return pattern;
    });

  // Determine if this is a test file config
  const isTestFile = newFiles[0]?.includes('test/');

  // Update configs to handle shared-types directory structure
  let updatedConfig = { ...config, files: newFiles };

  if (config.languageOptions) {
    const updatedLanguageOptions = { ...config.languageOptions };

    // Add Jest globals for test files
    if (isTestFile) {
      updatedLanguageOptions.globals = { ...updatedLanguageOptions.globals, ...jestGlobals };
    }

    // Update parserOptions to use our tsconfig with proper tsconfigRootDir
    if (config.languageOptions.parserOptions) {
      updatedLanguageOptions.parserOptions = {
        ...config.languageOptions.parserOptions,
        tsconfigRootDir: __dirname,
      };
    }

    updatedConfig = { ...updatedConfig, languageOptions: updatedLanguageOptions };
  }

  return [updatedConfig];
});

export default [
  {
    ignores: ['dist', 'out-tsc', 'node_modules'],
  },
  ...mappedConfigs,
  // Disable vitest rules for this Jest project
  {
    files: ['shared-types/test/**/*.spec.ts'],
    rules: {
      'vitest/consistent-test-filename': 'off',
      'vitest/consistent-test-it': 'off',
      'vitest/expect-expect': 'off',
      'vitest/no-alias-methods': 'off',
      'vitest/no-commented-out-tests': 'off',
      'vitest/no-conditional-expect': 'off',
      'vitest/no-conditional-in-test': 'off',
      'vitest/no-conditional-tests': 'off',
      'vitest/no-disabled-tests': 'off',
      'vitest/no-duplicate-hooks': 'off',
      'vitest/no-focused-tests': 'off',
      'vitest/no-identical-title': 'off',
      'vitest/no-standalone-expect': 'off',
      'vitest/no-test-prefixes': 'off',
      'vitest/no-test-return-statement': 'off',
      'vitest/prefer-comparison-matcher': 'off',
      'vitest/prefer-each': 'off',
      'vitest/prefer-equality-matcher': 'off',
      'vitest/prefer-expect-assertions': 'off',
      'vitest/prefer-expect-resolves': 'off',
      'vitest/prefer-hooks-in-order': 'off',
      'vitest/prefer-hooks-on-top': 'off',
      'vitest/prefer-lowercase-title': 'off',
      'vitest/prefer-mock-promise-shorthand': 'off',
      'vitest/prefer-spy-on': 'off',
      'vitest/prefer-strict-equal': 'off',
      'vitest/prefer-to-be': 'off',
      'vitest/prefer-to-be-falsy': 'off',
      'vitest/prefer-to-be-object': 'off',
      'vitest/prefer-to-be-truthy': 'off',
      'vitest/prefer-to-contain': 'off',
      'vitest/prefer-to-have-length': 'off',
      'vitest/prefer-todo': 'off',
      'vitest/require-hook': 'off',
      'vitest/require-to-throw-message': 'off',
      'vitest/require-top-level-describe': 'off',
      'vitest/valid-describe-callback': 'off',
      'vitest/valid-expect': 'off',
      'vitest/valid-title': 'off',
    },
  },
];