import { hasPackageJsonDependency } from "../utils.js";
class TanstackRouterDetector {
  name = "tanstack-router";
  language = "typescript";
  filePattern = /\.(js|jsx|ts|tsx)$/;
  projectFiles = [];
  async detect(projectRoot, files) {
    this.projectFiles = files;
    return hasPackageJsonDependency(projectRoot, files, ["@tanstack/react-router"]);
  }
  async extractRoutes(filePath, content, ctx) {
    const routes = [];
    if (filePath.includes("routes/")) {
      const routesDirIndex = filePath.indexOf("routes/");
      let routePart = filePath.substring(routesDirIndex + 7);
      routePart = routePart.replace(/\.[a-zA-Z0-9]+$/, "");
      let routePath = "/" + routePart.replace(/\./g, "/");
      routePath = routePath.replace(/\/index$/, "");
      if (routePath === "") routePath = "/";
      routePath = routePath.replace(/\$([a-zA-Z0-9_]+)/g, "{$1}");
      routes.push({
        framework: this.name,
        method: "GET",
        path: routePath,
        handlerFile: filePath,
        handlerSymbol: "Route",
        metadata: {
          confidence: "inferred",
          routeType: "client"
        }
      });
    }
    const routeRegex = /new\s+Route\s*\(\s*\{([^}]+)\}/g;
    let match;
    while ((match = routeRegex.exec(content)) !== null) {
      const configBody = match[1];
      const pathMatch = configBody.match(/\bpath\s*:\s*['"]([^'"]+)['"]/);
      const compMatch = configBody.match(/\bcomponent\s*:\s*([a-zA-Z0-9_]+)/);
      if (pathMatch) {
        const routePath = pathMatch[1];
        const componentSymbol = compMatch ? compMatch[1] : void 0;
        let resolvedFile = filePath;
        if (componentSymbol) {
          const resolvedPath = ctx.resolveSymbolToFile(componentSymbol);
          if (resolvedPath) {
            resolvedFile = resolvedPath;
          }
        }
        routes.push({
          framework: this.name,
          method: "GET",
          path: routePath,
          handlerFile: resolvedFile,
          handlerSymbol: componentSymbol || "Route",
          metadata: {
            confidence: "inferred",
            routeType: "client"
          }
        });
      }
    }
    return routes;
  }
}
export {
  TanstackRouterDetector
};
