import fs from 'node:fs/promises';
import path from 'node:path';
import { parseTerms } from './cli/helpers.js';
const resolveDictionaries = async () => {
    const dictionariesDir = path.resolve('.cspell');
    try {
        if (!(await fs.stat(dictionariesDir)).isDirectory()) {
            return;
        }
    }
    catch {
        return;
    }
    const dictionaries = [];
    const dictionaryDefinitions = [];
    for (const file of await fs.readdir(dictionariesDir, {
        withFileTypes: true,
    })) {
        if (!file.isFile()) {
            continue;
        }
        const dictionary = path.parse(file.name).name;
        dictionaries.push(dictionary);
        dictionaryDefinitions.push({
            name: dictionary,
            path: `.cspell/${file.name}`,
            addWords: true,
        });
    }
    return { dictionaries, dictionaryDefinitions };
};
const CASE_SENSITIVE_DICTIONARY = '$$case-sensitive$$';
export default async function doom() {
    const parsedTerms = await parseTerms();
    const words = [];
    const caseSensitiveWords = [];
    const flagWords = parsedTerms.flatMap((it) => {
        const word = it.en;
        const badCases = it.badCases?.en ?? [];
        if (badCases.some((c) => word !== c && word.toLowerCase() === c.toLowerCase())) {
            caseSensitiveWords.push(it);
            return [];
        }
        words.push(word);
        return badCases.map((c) => `${c}->${word}`);
    });
    const { dictionaries = [], dictionaryDefinitions = [] } = (await resolveDictionaries()) ?? {};
    if (caseSensitiveWords.length) {
        dictionaries.push(CASE_SENSITIVE_DICTIONARY);
        dictionaryDefinitions.push({
            name: CASE_SENSITIVE_DICTIONARY,
            words: caseSensitiveWords.flatMap((it) => [
                it.en,
                ...(it.badCases?.en ?? []).map((c) => `!${c}`),
            ]),
        });
    }
    return {
        allowCompoundWords: true,
        language: 'en,en-GB,en-US',
        useGitignore: true,
        words,
        flagWords,
        dictionaries,
        dictionaryDefinitions,
    };
}
