import { ruleCreator } from "../_internal/rule-creator.js";
const isSetTypeReference = (node) => node.typeName.type === "Identifier" && node.typeName.name === "Set";
const collectMutableSetTypeNodes = (typeNode) => {
    if (typeNode.type === "TSIntersectionType" ||
        typeNode.type === "TSUnionType") {
        return typeNode.types.flatMap((subTypeNode) => collectMutableSetTypeNodes(subTypeNode));
    }
    if (typeNode.type !== "TSTypeReference" || !isSetTypeReference(typeNode)) {
        return [];
    }
    return [typeNode.typeName];
};
const buildReadonlySetFix = (node) => (fixer) => fixer.replaceText(node, "ReadonlySet");
/**
 * Require readonly set type aliases.
 */
const rule = ruleCreator({
    create: (context) => ({
        TSTypeAliasDeclaration: (node) => {
            const mutableSetTypeNodes = collectMutableSetTypeNodes(node.typeAnnotation);
            for (const mutableSetTypeNode of mutableSetTypeNodes) {
                const fix = buildReadonlySetFix(mutableSetTypeNode);
                context.report({
                    fix,
                    messageId: "forbidden",
                    node: mutableSetTypeNode,
                    suggest: [
                        {
                            fix,
                            messageId: "suggestRequireReadonlySetTypeAlias",
                        },
                    ],
                });
            }
        },
    }),
    defaultOptions: [],
    meta: {
        deprecated: false,
        docs: {
            deprecated: false,
            description: "require ReadonlySet type alias annotations.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/typescript-require-readonly-set-type-alias",
        },
        fixable: "code",
        hasSuggestions: true,
        messages: {
            forbidden: "Prefer readonly set type aliases.",
            suggestRequireReadonlySetTypeAlias: "Convert this type alias annotation to ReadonlySet.",
        },
        schema: [],
        type: "suggestion",
    },
    name: "typescript/require-readonly-set-type-alias",
});
export default rule;
//# sourceMappingURL=typescript-require-readonly-set-type-alias.js.map