import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import cspellRecommended from '@cspell/eslint-plugin/recommended';
import js from '@eslint/js';
import react from '@eslint-react/eslint-plugin';
import { merge } from 'es-toolkit/compat';
import * as mdx from 'eslint-plugin-mdx';
import globals from 'globals';
import tseslint from 'typescript-eslint';
import { loadConfig } from './cli/load-config.js';
const cjsRequire = createRequire(import.meta.url);
let remarkConfigPath;
try {
    remarkConfigPath = fileURLToPath(import.meta.resolve('./remarkrc.js'));
}
catch {
    remarkConfigPath = cjsRequire.resolve('./remarkrc.js');
}
async function doom(cspellOptionsOrRoot) {
    let cspellOptions;
    if (typeof cspellOptionsOrRoot === 'string' ||
        cspellOptionsOrRoot instanceof URL) {
        const { config } = await loadConfig(cspellOptionsOrRoot);
        cspellOptions = config.lint?.cspellOptions;
    }
    else {
        cspellOptions = cspellOptionsOrRoot;
    }
    const cspellEnabled = cspellOptions !== null;
    return tseslint.config([
        {
            ignores: ['**/.yarn', '**/dist', '**/lib', '**/node_modules'],
        },
        {
            extends: [
                js.configs.recommended,
                react.configs.recommended,
                mdx.configs.flat,
            ],
            languageOptions: {
                globals: globals.node,
            },
        },
        {
            files: ['**/en/**/*.{js,jsx,md,mdx,ts,tsx}'],
            extends: cspellEnabled ? [cspellRecommended] : [],
            languageOptions: {
                globals: globals.browser,
                parserOptions: {
                    remarkConfigPath,
                },
            },
            rules: cspellEnabled
                ? {
                    '@cspell/spellchecker': [
                        'error',
                        merge({ autoFix: true }, cspellOptions),
                    ],
                }
                : {},
        },
        {
            files: ['**/*.mdx'],
            rules: {
                'no-unused-expressions': 'off',
            },
        },
        {
            files: ['**/*.{ts,tsx}'],
            extends: [
                tseslint.configs.recommendedTypeChecked,
                react.configs['recommended-typescript'],
            ],
            rules: {
                '@typescript-eslint/no-misused-promises': 'off',
                '@typescript-eslint/no-non-null-assertion': 'off',
                '@typescript-eslint/restrict-template-expressions': [
                    'error',
                    { allowNumber: true },
                ],
                'prefer-const': ['error', { destructuring: 'all' }],
            },
            languageOptions: {
                parser: tseslint.parser,
                parserOptions: {
                    projectService: true,
                },
            },
        },
    ]);
}
export default doom;
