"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var react_exports = {};
__export(react_exports, {
  reactResolver: () => reactResolver
});
module.exports = __toCommonJS(react_exports);
const reactResolver = {
  name: "react",
  detect(context) {
    const packageJson = context.readFile("package.json");
    if (packageJson) {
      try {
        const pkg = JSON.parse(packageJson);
        const deps = { ...pkg.dependencies, ...pkg.devDependencies };
        if (deps.react || deps.next || deps["react-native"]) return true;
      } catch {
      }
    }
    const allFiles = context.getAllFiles();
    return allFiles.some((f) => f.endsWith(".jsx") || f.endsWith(".tsx"));
  },
  resolve(ref, context) {
    if (isPascalCase(ref.referenceName) && !isBuiltInType(ref.referenceName)) {
      const id = resolveComponent(ref.referenceName, ref.filePath, context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.8, resolvedBy: "framework" };
    }
    if (ref.referenceName.startsWith("use") && ref.referenceName.length > 3) {
      const id = resolveHook(ref.referenceName, context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.85, resolvedBy: "framework" };
    }
    if (ref.referenceName.endsWith("Context") || ref.referenceName.endsWith("Provider")) {
      const id = resolveContext(ref.referenceName, context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.8, resolvedBy: "framework" };
    }
    return null;
  },
  extractNodes(filePath, content) {
    const nodes = [];
    const now = Date.now();
    const componentPatterns = [
      /(?:export\s+)?function\s+([A-Z][a-zA-Z0-9]*)\s*\(/g,
      /(?:export\s+)?(?:const|let)\s+([A-Z][a-zA-Z0-9]*)\s*=\s*(?:\([^)]*\)|[a-zA-Z_][a-zA-Z0-9_]*)\s*=>/g,
      /(?:export\s+)?(?:const|let)\s+([A-Z][a-zA-Z0-9]*)\s*=\s*(?:React\.)?forwardRef/g,
      /(?:export\s+)?(?:const|let)\s+([A-Z][a-zA-Z0-9]*)\s*=\s*(?:React\.)?memo/g
    ];
    for (const pattern of componentPatterns) {
      let match;
      while ((match = pattern.exec(content)) !== null) {
        const [fullMatch, name] = match;
        const line = content.slice(0, match.index).split("\n").length;
        const afterMatch = content.slice(match.index + fullMatch.length, match.index + fullMatch.length + 500);
        const hasJSX = afterMatch.includes("<") && (afterMatch.includes("/>") || afterMatch.includes("</"));
        if (hasJSX) {
          nodes.push({
            id: `component:${filePath}:${name}:${line}`,
            kind: "component",
            name,
            qualifiedName: `${filePath}::${name}`,
            filePath,
            startLine: line,
            endLine: line,
            startColumn: 0,
            endColumn: fullMatch.length,
            language: filePath.endsWith(".tsx") ? "tsx" : "jsx",
            isExported: fullMatch.includes("export"),
            updatedAt: now
          });
        }
      }
    }
    const hookPattern = /(?:export\s+)?(?:function|const|let)\s+(use[A-Z][a-zA-Z0-9]*)\s*[=(]/g;
    let hookMatch;
    while ((hookMatch = hookPattern.exec(content)) !== null) {
      const [fullMatch, name] = hookMatch;
      const line = content.slice(0, hookMatch.index).split("\n").length;
      nodes.push({
        id: `hook:${filePath}:${name}:${line}`,
        kind: "function",
        name,
        qualifiedName: `${filePath}::${name}`,
        filePath,
        startLine: line,
        endLine: line,
        startColumn: 0,
        endColumn: fullMatch.length,
        language: filePath.endsWith(".ts") || filePath.endsWith(".tsx") ? "typescript" : "javascript",
        isExported: fullMatch.includes("export"),
        updatedAt: now
      });
    }
    if (filePath.includes("pages/") || filePath.includes("app/")) {
      if (content.includes("export default")) {
        const routePath = filePathToRoute(filePath);
        if (routePath) {
          const line = content.slice(0, content.indexOf("export default")).split("\n").length;
          nodes.push({
            id: `route:${filePath}:${routePath}:${line}`,
            kind: "route",
            name: routePath,
            qualifiedName: `${filePath}::route:${routePath}`,
            filePath,
            startLine: line,
            endLine: line,
            startColumn: 0,
            endColumn: 0,
            language: filePath.endsWith(".tsx") ? "tsx" : filePath.endsWith(".ts") ? "typescript" : "javascript",
            updatedAt: now
          });
        }
      }
    }
    return nodes;
  }
};
function isPascalCase(str) {
  return /^[A-Z][a-zA-Z0-9]*$/.test(str);
}
function isBuiltInType(name) {
  const builtIns = /* @__PURE__ */ new Set([
    "Array",
    "Boolean",
    "Date",
    "Error",
    "Function",
    "JSON",
    "Math",
    "Number",
    "Object",
    "Promise",
    "RegExp",
    "String",
    "Symbol",
    "Map",
    "Set",
    "WeakMap",
    "WeakSet",
    "React",
    "Component",
    "Fragment",
    "Suspense",
    "StrictMode"
  ]);
  return builtIns.has(name);
}
function resolveComponent(name, fromFile, context) {
  const fromDir = fromFile.substring(0, fromFile.lastIndexOf("/"));
  for (const file of context.getAllFiles().filter((f) => f.startsWith(fromDir))) {
    if (file.toLowerCase().includes(name.toLowerCase())) {
      const node = context.getNodesInFile(file).find(
        (n) => (n.kind === "component" || n.kind === "function" || n.kind === "class") && n.name === name
      );
      if (node) return node.id;
    }
  }
  const componentDirs = ["components", "src/components", "app/components", "pages", "src/pages", "views", "src/views"];
  for (const dir of componentDirs) {
    for (const file of context.getAllFiles()) {
      if (file.startsWith(dir) && file.toLowerCase().includes(name.toLowerCase())) {
        const node = context.getNodesInFile(file).find(
          (n) => (n.kind === "component" || n.kind === "function" || n.kind === "class") && n.name === name
        );
        if (node) return node.id;
      }
    }
  }
  return null;
}
function resolveHook(name, context) {
  const hookDirs = ["hooks", "src/hooks", "lib/hooks", "utils/hooks"];
  for (const dir of hookDirs) {
    for (const file of context.getAllFiles()) {
      if (file.startsWith(dir) || file.includes("/hooks/")) {
        const node2 = context.getNodesInFile(file).find((n) => n.kind === "function" && n.name === name);
        if (node2) return node2.id;
      }
    }
  }
  const node = context.getNodesByName(name).find((n) => n.kind === "function" && n.name.startsWith("use"));
  return node?.id ?? null;
}
function resolveContext(name, context) {
  const contextDirs = ["context", "contexts", "src/context", "src/contexts", "providers", "src/providers"];
  for (const dir of contextDirs) {
    for (const file of context.getAllFiles()) {
      if (file.startsWith(dir) || file.includes("/context/") || file.includes("/contexts/")) {
        const node = context.getNodesInFile(file).find(
          (n) => n.name === name || n.name === name.replace(/Context$|Provider$/, "")
        );
        if (node) return node.id;
      }
    }
  }
  return null;
}
function filePathToRoute(filePath) {
  if (filePath.includes("pages/")) {
    let route = filePath.replace(/^.*pages\//, "/").replace(/\/index\.(tsx?|jsx?)$/, "").replace(/\.(tsx?|jsx?)$/, "").replace(/\[([^\]]+)\]/g, ":$1");
    if (route === "") route = "/";
    return route;
  }
  if (filePath.includes("app/")) {
    if (!filePath.includes("page.")) return null;
    let route = filePath.replace(/^.*app\//, "/").replace(/\/page\.(tsx?|jsx?)$/, "").replace(/\[([^\]]+)\]/g, ":$1");
    if (route === "") route = "/";
    return route.replace(/\/$/, "");
  }
  return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  reactResolver
});
//# sourceMappingURL=react.js.map
