import { isDefined } from "ts-extras";
import { ruleCreator } from "../_internal/rule-creator.js";
import { normalizeSyntaxSelector, } from "../_internal/syntax-selectors.js";
/**
 * Require at least one occurrence of each configured syntax selector.
 */
const rule = ruleCreator({
    create: (context, [options]) => {
        const entries = (options.selectors ?? []).map((selector) => normalizeSyntaxSelector(selector));
        const counters = entries.map(() => 0);
        let selectorListeners = {};
        for (const [index, entry] of entries.entries()) {
            selectorListeners = {
                ...selectorListeners,
                [entry.selector]: () => {
                    const count = counters[index] ?? 0;
                    counters[index] = count + 1;
                },
            };
        }
        return {
            ...selectorListeners,
            "Program:exit": (node) => {
                for (const [index, entry] of entries.entries()) {
                    const count = counters[index] ?? 0;
                    if (count > 0) {
                        continue;
                    }
                    if (isDefined(entry.message)) {
                        context.report({
                            data: {
                                message: entry.message,
                            },
                            messageId: "customMessage",
                            node,
                        });
                        continue;
                    }
                    context.report({
                        data: {
                            selector: entry.selector,
                        },
                        messageId: "missing",
                        node,
                    });
                }
            },
        };
    },
    defaultOptions: [{ selectors: [] }],
    meta: {
        defaultOptions: [{ selectors: [] }],
        deprecated: false,
        docs: {
            deprecated: false,
            description: "require at least one match for each configured AST selector.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/require-syntax",
        },
        hasSuggestions: false,
        messages: {
            customMessage: "{{message}}",
            missing: "Required syntax '{{selector}}' was not found.",
        },
        schema: [
            {
                additionalProperties: false,
                description: "Configuration for syntax selectors that must appear at least once.",
                properties: {
                    selectors: {
                        description: "Selector list. Each entry can be a selector string or a selector/message object.",
                        items: {
                            oneOf: [
                                {
                                    minLength: 1,
                                    type: "string",
                                },
                                {
                                    additionalProperties: false,
                                    properties: {
                                        message: {
                                            minLength: 1,
                                            type: "string",
                                        },
                                        selector: {
                                            minLength: 1,
                                            type: "string",
                                        },
                                    },
                                    required: ["selector"],
                                    type: "object",
                                },
                            ],
                        },
                        type: "array",
                    },
                },
                type: "object",
            },
        ],
        type: "suggestion",
    },
    name: "require-syntax",
});
export default rule;
//# sourceMappingURL=require-syntax.js.map