import { ruleCreator } from "../_internal/rule-creator.js";
const functionLikeNodeSelector = ":matches(ArrowFunctionExpression, FunctionDeclaration, FunctionExpression, TSCallSignatureDeclaration, TSConstructSignatureDeclaration, TSConstructorType, TSDeclareFunction, TSEmptyBodyFunctionExpression, TSFunctionType, TSMethodSignature)";
const getParametersFromFunctionLikeNode = (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.params;
    }
    return undefined;
};
const getTypeAnnotationFromPattern = (pattern) => {
    if (pattern.type === "AssignmentPattern") {
        return getTypeAnnotationFromPattern(pattern.left);
    }
    if (pattern.type === "RestElement") {
        if (pattern.typeAnnotation !== undefined) {
            return pattern.typeAnnotation;
        }
        const argument = pattern.argument;
        if (argument.type !== "ArrayPattern" &&
            argument.type !== "Identifier" &&
            argument.type !== "ObjectPattern") {
            return undefined;
        }
        return argument.typeAnnotation;
    }
    return pattern.typeAnnotation;
};
const getTypeAnnotationFromParameter = (parameter) => {
    if (parameter.type === "TSParameterProperty") {
        return getTypeAnnotationFromPattern(parameter.parameter);
    }
    return getTypeAnnotationFromPattern(parameter);
};
const isArrayTypeReference = (node) => node.typeName.type === "Identifier" && node.typeName.name === "Array";
const collectMutableArrayLikeTypeNodes = (typeNode) => {
    if (typeNode.type === "TSArrayType" || typeNode.type === "TSTupleType") {
        return [typeNode];
    }
    if (typeNode.type === "TSIntersectionType" ||
        typeNode.type === "TSUnionType") {
        return typeNode.types.flatMap((subTypeNode) => collectMutableArrayLikeTypeNodes(subTypeNode));
    }
    if (typeNode.type === "TSTypeReference") {
        if (!isArrayTypeReference(typeNode)) {
            return [];
        }
        return [typeNode.typeName];
    }
    return [];
};
const buildReadonlyArrayLikeFix = (node, sourceCode) => {
    if (node.type === "Identifier") {
        return (fixer) => fixer.replaceText(node, "ReadonlyArray");
    }
    return (fixer) => fixer.replaceText(node, `readonly ${sourceCode.getText(node)}`);
};
/**
 * Prefer readonly array-like parameter type annotations.
 */
const rule = ruleCreator({
    create: (context) => {
        const sourceCode = context.sourceCode;
        return {
            [functionLikeNodeSelector]: (node) => {
                const parameters = getParametersFromFunctionLikeNode(node);
                if (parameters === undefined) {
                    return;
                }
                for (const parameter of parameters) {
                    const typeAnnotation = getTypeAnnotationFromParameter(parameter);
                    if (typeAnnotation === undefined) {
                        continue;
                    }
                    const mutableArrayLikeTypeNodes = collectMutableArrayLikeTypeNodes(typeAnnotation.typeAnnotation);
                    for (const mutableArrayLikeTypeNode of mutableArrayLikeTypeNodes) {
                        const fix = buildReadonlyArrayLikeFix(mutableArrayLikeTypeNode, sourceCode);
                        context.report({
                            fix,
                            messageId: "forbidden",
                            node: mutableArrayLikeTypeNode,
                            suggest: [
                                {
                                    fix,
                                    messageId: "suggestPreferReadonlyArrayParameter",
                                },
                            ],
                        });
                    }
                }
            },
        };
    },
    defaultOptions: [],
    meta: {
        deprecated: false,
        docs: {
            deprecated: false,
            description: "require readonly array and tuple parameter type annotations.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/typescript-prefer-readonly-array-parameter",
        },
        fixable: "code",
        hasSuggestions: true,
        messages: {
            forbidden: "Prefer readonly array-like parameter types.",
            suggestPreferReadonlyArrayParameter: "Convert this parameter type to a readonly array-like form.",
        },
        schema: [],
        type: "suggestion",
    },
    name: "typescript/prefer-readonly-array-parameter",
});
export default rule;
//# sourceMappingURL=typescript-prefer-readonly-array-parameter.js.map