import { isDefined } from "ts-extras";
import { getImportSourceFromNode, shouldReportImportSource, } from "./import-patterns.js";
import { ruleCreator } from "./rule-creator.js";
const createImportVisitors = (context, defaultDisallowPatterns) => ({
    "ImportDeclaration, ExportNamedDeclaration[source], ExportAllDeclaration, ImportExpression": (node) => {
        const sourceText = getImportSourceFromNode(node);
        if (!isDefined(sourceText)) {
            return;
        }
        const [options = {}] = context.options;
        if (!shouldReportImportSource(sourceText, options, defaultDisallowPatterns)) {
            return;
        }
        context.report({
            data: {
                source: sourceText,
            },
            messageId: "disallowedSource",
            node,
        });
    },
});
/**
 * Creates a rule that disallows import/export sources by glob pattern.
 *
 * @param options - Rule creation options.
 *
 * @returns ESLint rule module.
 */
export const createImportPatternRule = ({ defaultDisallowPatterns, description, name, }) => ruleCreator({
    create: (context) => createImportVisitors(context, defaultDisallowPatterns),
    defaultOptions: [{}],
    meta: {
        defaultOptions: [{}],
        docs: {
            description,
            recommended: false,
            url: `https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/${name.replaceAll("/", "-")}`,
        },
        hasSuggestions: false,
        messages: {
            disallowedSource: "Import source `{{ source }}` is disallowed by this rule.",
        },
        schema: [
            {
                additionalProperties: false,
                properties: {
                    allow: {
                        items: {
                            type: "string",
                        },
                        type: "array",
                    },
                    disallow: {
                        items: {
                            type: "string",
                        },
                        type: "array",
                    },
                },
                type: "object",
            },
        ],
        type: "problem",
    },
    name,
});
//# sourceMappingURL=create-import-pattern-rule.js.map