import { arrayFirst } from "ts-extras";
import { ruleCreator } from "../_internal/rule-creator.js";
const defaultOptions = [{}];
const isExportedEnumDeclaration = (node) => node.parent?.type === "ExportNamedDeclaration";
const getConstToken = (sourceCode, node) => sourceCode.getFirstToken(node, {
    filter: (token) => token.value === "const",
}) ?? undefined;
/**
 * Disallow `const enum` declarations.
 */
const rule = ruleCreator({
    create: (context) => {
        const sourceCode = context.sourceCode;
        return {
            TSEnumDeclaration: (node) => {
                if (!node.const) {
                    return;
                }
                const [{ allowLocal = false } = {}] = context.options;
                if (allowLocal && !isExportedEnumDeclaration(node)) {
                    return;
                }
                const constToken = getConstToken(sourceCode, node);
                const fix = constToken === undefined
                    ? undefined
                    : (fixer) => {
                        let removeEnd = constToken.range[1];
                        while (removeEnd < sourceCode.text.length &&
                            (sourceCode.text.at(removeEnd) === " " ||
                                sourceCode.text.at(removeEnd) === "\t")) {
                            removeEnd += 1;
                        }
                        return fixer.removeRange([
                            arrayFirst(constToken.range),
                            removeEnd,
                        ]);
                    };
                context.report({
                    messageId: "forbidden",
                    node: node.id,
                    ...(fix === undefined
                        ? {}
                        : {
                            fix,
                            suggest: [
                                {
                                    fix,
                                    messageId: "suggestRemoveConst",
                                },
                            ],
                        }),
                });
            },
        };
    },
    defaultOptions,
    meta: {
        defaultOptions: [{}],
        deprecated: false,
        docs: {
            deprecated: false,
            description: "disallow const enum declarations.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/no-const-enum",
        },
        fixable: "code",
        hasSuggestions: true,
        messages: {
            forbidden: "`const enum` declarations are forbidden.",
            suggestRemoveConst: "Remove the const modifier from this enum declaration.",
        },
        schema: [
            {
                additionalProperties: false,
                description: "Configuration for allowing non-exported const enum declarations.",
                properties: {
                    allowLocal: {
                        description: "Allow const enum declarations when they are not exported.",
                        type: "boolean",
                    },
                },
                type: "object",
            },
        ],
        type: "problem",
    },
    name: "no-const-enum",
});
export default rule;
//# sourceMappingURL=no-const-enum.js.map