import { rulesNames } from './rules.js';
import EnumIssue from './issues/enum-issue.js';
let issues;
export function checkEnums(enumDeclarations, fieldDeclarations) {
    if (!enumDeclarations) {
        return [];
    }
    issues = [];
    checkForDuplicatedEnums(enumDeclarations);
    checkForUnusedEnums(enumDeclarations, fieldDeclarations);
    return issues;
}
function checkForDuplicatedEnums(enumDeclarations) {
    const enumNames = new Set();
    const duplicatedEnumIssues = new Map();
    enumDeclarations.forEach(enumDeclaration => {
        const enumName = enumDeclaration.children.NAME[0].image;
        if (enumNames.has(enumName)) {
            if (!duplicatedEnumIssues.has(enumName)) {
                duplicatedEnumIssues.set(enumName, new EnumIssue({
                    ruleName: rulesNames.ENUM_DUPLICATED,
                    enumName,
                }));
            }
        }
        else {
            enumNames.add(enumName);
        }
    });
    duplicatedEnumIssues.forEach(issue => {
        issues.push(issue);
    });
}
function checkForUnusedEnums(enumDeclarations, fieldDeclarations) {
    const fieldTypes = fieldDeclarations.map(fieldDeclaration => {
        return fieldDeclaration.children.type[0].children.NAME[0].image;
    });
    const declaredEnums = new Set(enumDeclarations.map(enumDeclaration => {
        return enumDeclaration.children.NAME[0].image;
    }));
    fieldTypes.forEach(usedEnum => {
        declaredEnums.delete(usedEnum);
    });
    if (declaredEnums.size !== 0) {
        declaredEnums.forEach(unusedEnum => {
            issues.push(new EnumIssue({ enumName: unusedEnum, ruleName: rulesNames.ENUM_UNUSED }));
        });
    }
}
