import { ruleCreator } from "../_internal/rule-creator.js";
const functionLikeNodeSelector = ":matches(ArrowFunctionExpression, FunctionDeclaration, FunctionExpression, TSCallSignatureDeclaration, TSConstructSignatureDeclaration, TSConstructorType, TSDeclareFunction, TSEmptyBodyFunctionExpression, TSFunctionType, TSMethodSignature)";
const getReturnTypeAnnotationFromFunctionLikeNode = (node) => {
    if (node.type === "ArrowFunctionExpression" ||
        node.type === "FunctionDeclaration" ||
        node.type === "FunctionExpression" ||
        node.type === "TSCallSignatureDeclaration" ||
        node.type === "TSConstructSignatureDeclaration" ||
        node.type === "TSConstructorType" ||
        node.type === "TSDeclareFunction" ||
        node.type === "TSEmptyBodyFunctionExpression" ||
        node.type === "TSFunctionType" ||
        node.type === "TSMethodSignature") {
        return node.returnType;
    }
    return undefined;
};
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 return type annotations.
 */
const rule = ruleCreator({
    create: (context) => ({
        [functionLikeNodeSelector]: (node) => {
            const returnTypeAnnotation = getReturnTypeAnnotationFromFunctionLikeNode(node);
            if (returnTypeAnnotation === undefined) {
                return;
            }
            const mutableMapTypeNodes = collectMutableMapTypeNodes(returnTypeAnnotation.typeAnnotation);
            for (const mutableMapTypeNode of mutableMapTypeNodes) {
                const fix = buildReadonlyMapFix(mutableMapTypeNode);
                context.report({
                    fix,
                    messageId: "forbidden",
                    node: mutableMapTypeNode,
                    suggest: [
                        {
                            fix,
                            messageId: "suggestRequireReadonlyMapReturnType",
                        },
                    ],
                });
            }
        },
    }),
    defaultOptions: [],
    meta: {
        deprecated: false,
        docs: {
            deprecated: false,
            description: "require ReadonlyMap return type annotations.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/typescript-require-readonly-map-return-type",
        },
        fixable: "code",
        hasSuggestions: true,
        messages: {
            forbidden: "Prefer readonly map return types.",
            suggestRequireReadonlyMapReturnType: "Convert this return type to ReadonlyMap.",
        },
        schema: [],
        type: "suggestion",
    },
    name: "typescript/require-readonly-map-return-type",
});
export default rule;
//# sourceMappingURL=typescript-require-readonly-map-return-type.js.map