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