import { hasPackageJsonDependency } from "../utils.js";
class ReactRouterDetector {
  name = "react-router";
  language = "typescript";
  filePattern = /\.(js|jsx|ts|tsx)$/;
  async detect(projectRoot, files) {
    return hasPackageJsonDependency(projectRoot, files, ["react-router", "react-router-dom"]);
  }
  async extractRoutes(filePath, content, ctx) {
    const routes = [];
    if (!content.includes("Route") && !content.includes("createBrowserRouter")) {
      return [];
    }
    const tagRegex = /<\/?Route\b|path=['"]([^'"]*)['"]|element=\{\s*<\s*([a-zA-Z0-9_]+)/g;
    let match;
    const pathStack = [];
    let currentPath = null;
    let currentElement = null;
    let inOpeningTag = false;
    const lines = content.split("\n");
    for (const line of lines) {
      if (line.includes("<Route")) {
        const pathMatch = line.match(/\bpath=['"]([^'"]*)['"]/);
        const elementMatch = line.match(/\belement=\{\s*<\s*([a-zA-Z0-9_]+)/);
        const pathSeg = pathMatch ? pathMatch[1] : "";
        const element = elementMatch ? elementMatch[1] : null;
        if (line.trim().endsWith("/>")) {
          const fullPath = "/" + [...pathStack, pathSeg].map((p) => p.replace(/^\/|\/$/g, "")).filter(Boolean).join("/");
          if (element) {
            let resolvedFile = filePath;
            const resolvedPath = ctx.resolveSymbolToFile(element);
            if (resolvedPath) {
              resolvedFile = resolvedPath;
            }
            routes.push({
              framework: this.name,
              method: "GET",
              path: fullPath,
              handlerFile: resolvedFile,
              handlerSymbol: element,
              metadata: {
                confidence: "inferred",
                routeType: "client"
              }
            });
          }
        } else {
          pathStack.push(pathSeg);
          if (element) {
            const fullPath = "/" + pathStack.map((p) => p.replace(/^\/|\/$/g, "")).filter(Boolean).join("/");
            let resolvedFile = filePath;
            const resolvedPath = ctx.resolveSymbolToFile(element);
            if (resolvedPath) {
              resolvedFile = resolvedPath;
            }
            routes.push({
              framework: this.name,
              method: "GET",
              path: fullPath,
              handlerFile: resolvedFile,
              handlerSymbol: element,
              metadata: {
                confidence: "inferred",
                routeType: "client"
              }
            });
          }
        }
      } else if (line.includes("</Route>")) {
        pathStack.pop();
      }
    }
    const createRouterRegex = /createBrowserRouter\s*\(/;
    if (createRouterRegex.test(content)) {
      const configRegex = /path\s*:\s*['"]([^'"]+)['"]\s*,\s*element\s*:\s*<\s*([a-zA-Z0-9_]+)/g;
      while ((match = configRegex.exec(content)) !== null) {
        const routePath = match[1];
        const element = match[2];
        let resolvedFile = filePath;
        const resolvedPath = ctx.resolveSymbolToFile(element);
        if (resolvedPath) {
          resolvedFile = resolvedPath;
        }
        routes.push({
          framework: this.name,
          method: "GET",
          path: routePath,
          handlerFile: resolvedFile,
          handlerSymbol: element,
          metadata: {
            confidence: "inferred",
            routeType: "client"
          }
        });
      }
    }
    return routes;
  }
}
export {
  ReactRouterDetector
};
