import { ruleCreator } from "../_internal/rule-creator.js";
const internalTagPattern = /@internal\b/u;
const isNonUnderscoreIdentifier = (identifier) => !identifier.name.startsWith("_");
const isExportDeclaration = (node) => node?.type === "ExportDefaultDeclaration" ||
    node?.type === "ExportNamedDeclaration";
/**
 * Enforce underscore prefixes for declarations marked with `@internal`.
 */
const rule = ruleCreator({
    create: (context) => {
        const hasInternalTag = (node) => {
            let commentTargets = [node];
            if (isExportDeclaration(node.parent)) {
                commentTargets = [...commentTargets, node.parent];
            }
            return commentTargets.some((commentTarget) => context.sourceCode
                .getCommentsBefore(commentTarget)
                .some((comment) => internalTagPattern.test(comment.value)));
        };
        const reportIfInternal = (nameIdentifier, commentTarget) => {
            if (!isNonUnderscoreIdentifier(nameIdentifier)) {
                return;
            }
            if (!hasInternalTag(commentTarget)) {
                return;
            }
            context.report({
                messageId: "forbidden",
                node: nameIdentifier,
            });
        };
        return {
            "ClassDeclaration[id.type='Identifier']": (node) => {
                if (node.id === null) {
                    return;
                }
                reportIfInternal(node.id, node);
            },
            "FunctionDeclaration[id.type='Identifier']": (node) => {
                if (node.id === null) {
                    return;
                }
                reportIfInternal(node.id, node);
            },
            "MethodDefinition[key.type='Identifier']": (node) => {
                if (node.key.type !== "Identifier") {
                    return;
                }
                reportIfInternal(node.key, node);
            },
            "PropertyDefinition[key.type='Identifier']": (node) => {
                if (node.key.type !== "Identifier") {
                    return;
                }
                reportIfInternal(node.key, node);
            },
            "TSEnumDeclaration[id.type='Identifier']": (node) => {
                reportIfInternal(node.id, node);
            },
            "TSEnumMember[id.type='Identifier']": (node) => {
                if (node.id.type !== "Identifier") {
                    return;
                }
                reportIfInternal(node.id, node);
            },
            "TSInterfaceDeclaration[id.type='Identifier']": (node) => {
                reportIfInternal(node.id, node);
            },
            "TSMethodSignature[key.type='Identifier']": (node) => {
                if (node.key.type !== "Identifier") {
                    return;
                }
                reportIfInternal(node.key, node);
            },
            "TSPropertySignature[key.type='Identifier']": (node) => {
                if (node.key.type !== "Identifier") {
                    return;
                }
                reportIfInternal(node.key, node);
            },
            "TSTypeAliasDeclaration[id.type='Identifier']": (node) => {
                reportIfInternal(node.id, node);
            },
            "VariableDeclarator[id.type='Identifier']": (node) => {
                if (node.id.type !== "Identifier") {
                    return;
                }
                const declaration = node.parent;
                if (declaration?.type !== "VariableDeclaration") {
                    return;
                }
                reportIfInternal(node.id, declaration);
            },
        };
    },
    defaultOptions: [],
    meta: {
        deprecated: false,
        docs: {
            deprecated: false,
            description: "disallow internal APIs that are not prefixed with underscores.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/underscore-internal",
        },
        hasSuggestions: false,
        messages: {
            forbidden: "Internal APIs not prefixed with underscores are forbidden.",
        },
        schema: [],
        type: "problem",
    },
    name: "underscore-internal",
});
export default rule;
//# sourceMappingURL=underscore-internal.js.map