"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasReactDependency = hasReactDependency;
const devkit_1 = require("@nx/devkit");
/**
 * React-related dependency patterns.
 * Matches actual React ecosystem packages, not workspace projects
 * that happen to have "react" in their name.
 */
function isReactRelatedDependency(dep) {
    return (
    // Exact match for "react" package
    dep === 'react' ||
        // react-* packages (react-dom, react-is, react-refresh, etc.)
        dep.startsWith('react-') ||
        // @types/react packages
        dep.startsWith('@types/react') ||
        // @mui/* pattern
        dep.startsWith('@mui/') ||
        // @kaltura/ds* pattern
        dep.startsWith('@kaltura/ds'));
}
/**
 * Extract package name from Nx dependency target.
 * Handles both workspace projects and npm packages (npm:package-name format).
 */
function getPackageName(target, graph) {
    // Check if it's an external npm package
    const externalNode = graph.externalNodes?.[target];
    if (externalNode) {
        return externalNode.data?.packageName || target.replace(/^npm:/, '');
    }
    return target;
}
/**
 * Check if a project has React dependencies, including transitive dependencies
 * through workspace projects only.
 *
 * Uses Nx's project graph to traverse dependencies. Only traverses into
 * workspace projects' transitive dependencies, not external npm packages.
 * This prevents false positives from build tools (like @vitejs/plugin-react)
 * that have transitive dependencies on react-refresh, react-is, etc.
 *
 * @param projectName - The Nx project name to check
 * @returns true if the project or any transitive workspace dependency depends on React
 */
async function hasReactDependency(projectName) {
    const graph = await (0, devkit_1.createProjectGraphAsync)();
    const visited = new Set();
    function traverse(current) {
        if (visited.has(current)) {
            return false;
        }
        visited.add(current);
        const deps = graph.dependencies[current] || [];
        for (const dep of deps) {
            const packageName = getPackageName(dep.target, graph);
            if (isReactRelatedDependency(packageName)) {
                return true;
            }
            // Only recursively check dependencies for workspace projects, not external npm packages.
            // External npm packages (like @vitejs/plugin-react, vitest) have their own transitive deps
            // (like react-refresh, react-is) which are build tools, not actual runtime dependencies.
            const isDepExternal = !!graph.externalNodes?.[dep.target];
            if (!isDepExternal && traverse(dep.target)) {
                return true;
            }
        }
        return false;
    }
    return traverse(projectName);
}
