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");
const isTopLevelPropertySignature = (node) => {
    const parent = node.parent;
    if (parent?.type === "TSInterfaceBody") {
        return true;
    }
    if (parent?.type !== "TSTypeLiteral") {
        return false;
    }
    const maybeTypeAliasDeclaration = parent.parent;
    return maybeTypeAliasDeclaration?.type === "TSTypeAliasDeclaration";
};
/**
 * Require readonly map property type annotations.
 */
const rule = ruleCreator({
    create: (context) => ({
        "TSPropertySignature[typeAnnotation!=null]": (node) => {
            if (!isTopLevelPropertySignature(node)) {
                return;
            }
            const typeAnnotation = node.typeAnnotation;
            if (typeAnnotation === undefined) {
                return;
            }
            const mutableMapTypeNodes = collectMutableMapTypeNodes(typeAnnotation.typeAnnotation);
            for (const mutableMapTypeNode of mutableMapTypeNodes) {
                const fix = buildReadonlyMapFix(mutableMapTypeNode);
                context.report({
                    fix,
                    messageId: "forbidden",
                    node: mutableMapTypeNode,
                    suggest: [
                        {
                            fix,
                            messageId: "suggestRequireReadonlyMapPropertyType",
                        },
                    ],
                });
            }
        },
    }),
    defaultOptions: [],
    meta: {
        deprecated: false,
        docs: {
            deprecated: false,
            description: "require ReadonlyMap property type annotations.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/typescript-require-readonly-map-property-type",
        },
        fixable: "code",
        hasSuggestions: true,
        messages: {
            forbidden: "Prefer readonly map property types.",
            suggestRequireReadonlyMapPropertyType: "Convert this property type to ReadonlyMap.",
        },
        schema: [],
        type: "suggestion",
    },
    name: "typescript/require-readonly-map-property-type",
});
export default rule;
//# sourceMappingURL=typescript-require-readonly-map-property-type.js.map