/**
 * Circular dependency detection
 */
import * as path from "node:path";
import { createDiagnostic } from "../types/diagnostic.js";
import { ok, error } from "../types/result.js";
/**
 * Check for circular dependencies using depth-first search
 */
export const checkCircularDependencies = (dependencies) => {
    const visited = new Set();
    const stack = new Set();
    const visit = (module, path) => {
        if (stack.has(module)) {
            return [...path, module]; // Found cycle
        }
        if (visited.has(module)) {
            return null; // Already checked
        }
        visited.add(module);
        stack.add(module);
        const deps = dependencies.get(module) ?? [];
        for (const dep of deps) {
            const cycle = visit(dep, [...path, module]);
            if (cycle) {
                return cycle;
            }
        }
        stack.delete(module);
        return null;
    };
    for (const [module] of dependencies) {
        const cycle = visit(module, []);
        if (cycle) {
            return error(createDiagnostic("TSN1002", "error", `Circular dependency detected: ${cycle.map((m) => path.basename(m)).join(" → ")}`, undefined, "Break the circular dependency by refactoring shared code"));
        }
    }
    return ok(undefined);
};
//# sourceMappingURL=circular.js.map