import { arrayJoin, isDefined, isEmpty } from "ts-extras";
import { ruleCreator } from "../_internal/rule-creator.js";
const selector = arrayJoin([
    "Identifier[optional=true] > TSTypeAnnotation > TSUnionType",
    "TSNamedTupleMember[optional=true] > TSUnionType",
    "TSOptionalType > TSUnionType",
], ", ");
const buildFixedTypeText = (sourceCode, unionType) => {
    let nonUndefinedTypeTexts = [];
    for (const typeNode of unionType.types) {
        if (typeNode.type === "TSUndefinedKeyword") {
            continue;
        }
        nonUndefinedTypeTexts = [
            ...nonUndefinedTypeTexts,
            sourceCode.getText(typeNode),
        ];
    }
    if (isEmpty(nonUndefinedTypeTexts) ||
        nonUndefinedTypeTexts.length === unionType.types.length) {
        return undefined;
    }
    return arrayJoin(nonUndefinedTypeTexts, " | ");
};
/**
 * Disallow redundant `undefined` unions on optional parameters and tuple
 * members.
 */
const rule = ruleCreator({
    create: (context) => {
        const sourceCode = context.sourceCode;
        return {
            [selector]: (node) => {
                if (node.type !== "TSUnionType") {
                    return;
                }
                const fixedTypeText = buildFixedTypeText(sourceCode, node);
                if (!isDefined(fixedTypeText)) {
                    return;
                }
                const fix = (fixer) => fixer.replaceText(node, fixedTypeText);
                context.report({
                    fix,
                    messageId: "forbidden",
                    node,
                    suggest: [
                        {
                            fix,
                            messageId: "suggestRemoveRedundantUndefined",
                        },
                    ],
                });
            },
        };
    },
    defaultOptions: [],
    meta: {
        deprecated: false,
        docs: {
            deprecated: false,
            description: "disallow redundant `undefined` in optional parameter and tuple member union types.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/typescript-no-redundant-undefined-optional",
        },
        fixable: "code",
        hasSuggestions: true,
        messages: {
            forbidden: "Optional parameters and tuple members should not redundantly include `undefined` in their type union.",
            suggestRemoveRedundantUndefined: "Remove redundant `undefined` from this optional union type.",
        },
        schema: [],
        type: "suggestion",
    },
    name: "typescript/no-redundant-undefined-optional",
});
export default rule;
//# sourceMappingURL=typescript-no-redundant-undefined-optional.js.map