import { dirname, resolve } from "node:path";
import { arrayIncludes } from "ts-extras";
import { getImportSourceFromNode } from "../_internal/import-patterns.js";
import { ruleCreator } from "../_internal/rule-creator.js";
import { createReplacementRuleInfo, withDeprecatedRuleLifecycle, } from "../_internal/rule-deprecation.js";
const importFileSuffixes = [
    "",
    ".ts",
    ".tsx",
    ".js",
    ".mjs",
    ".cjs",
    ".d.ts",
    "/index.ts",
    "/index.tsx",
    "/index.js",
    "/index.mjs",
    "/index.cjs",
];
const toResolvedCandidates = (baseDirectory, importSource) => importFileSuffixes.map((suffix) => resolve(baseDirectory, `${importSource}${suffix}`));
/**
 * Disallow importing the current file from itself.
 */
const rule = ruleCreator({
    create: (context) => {
        const currentFilePath = context.filename;
        if (currentFilePath === "<input>") {
            return {};
        }
        const normalizedCurrentFilePath = resolve(currentFilePath);
        const currentFileDirectory = dirname(normalizedCurrentFilePath);
        return {
            "ImportDeclaration, ExportNamedDeclaration[source], ExportAllDeclaration, ImportExpression": (node) => {
                const sourceText = getImportSourceFromNode(node);
                if (sourceText?.startsWith(".") !== true) {
                    return;
                }
                const candidatePaths = toResolvedCandidates(currentFileDirectory, sourceText);
                if (arrayIncludes(candidatePaths, normalizedCurrentFilePath)) {
                    context.report({
                        messageId: "forbidden",
                        node,
                    });
                }
            },
        };
    },
    defaultOptions: [],
    meta: {
        deprecated: true,
        docs: {
            deprecated: true,
            description: "disallow importing the current file from itself.",
            frozen: true,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/no-self-import",
        },
        hasSuggestions: false,
        messages: {
            forbidden: "Do not import the current file from itself.",
        },
        schema: [],
        type: "problem",
    },
    name: "no-self-import",
});
/**
 * Wrapper rule with explicit lifecycle metadata and replacement mapping.
 */
const deprecatedRule = withDeprecatedRuleLifecycle(rule, {
    message: "Deprecated in favor of import/no-self-import.",
    replacedBy: [
        createReplacementRuleInfo({
            plugin: {
                name: "import",
                url: "https://github.com/import-js/eslint-plugin-import",
            },
            rule: {
                name: "no-self-import",
                url: "https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-self-import.md",
            },
        }),
    ],
    ruleId: "no-self-import",
});
export default deprecatedRule;
//# sourceMappingURL=no-self-import.js.map