import { hasPackageJsonDependency } from "../utils.js";
class NextJSDetector {
  name = "nextjs";
  language = "typescript";
  filePattern = /\.(js|jsx|ts|tsx)$/;
  async detect(projectRoot, files) {
    return hasPackageJsonDependency(projectRoot, files, ["next"]);
  }
  async extractRoutes(filePath, content, ctx) {
    const routes = [];
    const pathNormalized = filePath.replace(/\\/g, "/");
    if (pathNormalized.includes("pages/")) {
      const pagesIndex = pathNormalized.indexOf("pages/");
      let routePart = pathNormalized.substring(pagesIndex + 6);
      routePart = routePart.replace(/\.[a-zA-Z0-9]+$/, "");
      let routePath = "/" + routePart;
      routePath = routePath.replace(/\/index$/, "");
      if (routePath === "") routePath = "/";
      routePath = routePath.replace(/\[([a-zA-Z0-9_]+)\]/g, "{$1}");
      const isApi = routePath.startsWith("/api/");
      routes.push({
        framework: this.name,
        method: isApi ? "ALL" : "GET",
        path: routePath,
        handlerFile: filePath,
        handlerSymbol: "default",
        metadata: {
          confidence: "inferred",
          routeType: isApi ? "server" : "client"
        }
      });
    }
    if (pathNormalized.endsWith("/page.tsx") || pathNormalized.endsWith("/page.jsx")) {
      const appIndex = pathNormalized.indexOf("app/");
      if (appIndex !== -1) {
        let routePart = pathNormalized.substring(appIndex + 4);
        routePart = routePart.replace(/\/page\.[a-zA-Z0-9]+$/, "");
        const segments = routePart.split("/").filter((seg) => !seg.startsWith("(") || !seg.endsWith(")"));
        let routePath = "/" + segments.join("/");
        if (routePath === "") routePath = "/";
        routePath = routePath.replace(/\[([a-zA-Z0-9_]+)\]/g, "{$1}");
        routes.push({
          framework: this.name,
          method: "GET",
          path: routePath,
          handlerFile: filePath,
          handlerSymbol: "default",
          metadata: {
            confidence: "inferred",
            routeType: "client"
          }
        });
      }
    }
    if (pathNormalized.endsWith("/route.ts") || pathNormalized.endsWith("/route.js")) {
      const appIndex = pathNormalized.indexOf("app/");
      if (appIndex !== -1) {
        let routePart = pathNormalized.substring(appIndex + 4);
        routePart = routePart.replace(/\/route\.[a-zA-Z0-9]+$/, "");
        const segments = routePart.split("/").filter((seg) => !seg.startsWith("(") || !seg.endsWith(")"));
        let routePath = "/" + segments.join("/");
        if (routePath === "") routePath = "/";
        routePath = routePath.replace(/\[([a-zA-Z0-9_]+)\]/g, "{$1}");
        const verbRegex = /export\s+(?:async\s+)?function\s+(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD)\b/g;
        let match;
        while ((match = verbRegex.exec(content)) !== null) {
          const verb = match[1];
          routes.push({
            framework: this.name,
            method: verb,
            path: routePath,
            handlerFile: filePath,
            handlerSymbol: verb,
            metadata: {
              confidence: "inferred",
              routeType: "server"
            }
          });
        }
      }
    }
    if (content.includes("'use server'") || content.includes('"use server"')) {
      const actionRegex = /export\s+(?:async\s+)?function\s+([a-zA-Z0-9_]+)\b/g;
      let match;
      while ((match = actionRegex.exec(content)) !== null) {
        const actionName = match[1];
        routes.push({
          framework: this.name,
          method: "POST",
          path: `/action/${actionName}`,
          handlerFile: filePath,
          handlerSymbol: actionName,
          metadata: {
            confidence: "inferred",
            routeType: "server"
          }
        });
      }
    }
    return routes;
  }
}
export {
  NextJSDetector
};
