import { join } from "node:path";
import { existsSync } from "node:fs";
class VaporDetector {
  name = "vapor";
  language = "swift";
  filePattern = /\.swift$/;
  async detect(projectRoot, files) {
    if (files.some((f) => f === "Package.swift" || f.endsWith("/Package.swift"))) {
      return true;
    }
    const pkgSwift = join(projectRoot, "Package.swift");
    return existsSync(pkgSwift);
  }
  async extractRoutes(filePath, content, ctx) {
    const routes = [];
    if (!content.includes("import Vapor") && !content.includes("routes(")) {
      return [];
    }
    const routeRegex = /\b[a-zA-Z0-9_]+\.(get|post|put|delete|patch)\s*\(\s*([^)]+)\s*,\s*use\s*:\s*([a-zA-Z0-9_.]+)\s*\)/g;
    let match;
    while ((match = routeRegex.exec(content)) !== null) {
      const verb = match[1].toUpperCase();
      const rawSegments = match[2];
      const handlerSymbol = match[3];
      const segments = rawSegments.split(",").map((s) => s.trim()).map((s) => s.replace(/['"]/g, "")).filter((s) => s !== "");
      const mappedSegments = segments.map((seg) => {
        if (seg.startsWith(":")) {
          return `{${seg.substring(1)}}`;
        }
        return seg;
      });
      const pathVal = "/" + mappedSegments.join("/");
      let resolvedFile = filePath;
      const resolvedPath = ctx.resolveSymbolToFile(handlerSymbol);
      if (resolvedPath) {
        resolvedFile = resolvedPath;
      }
      routes.push({
        framework: this.name,
        method: verb,
        path: pathVal,
        handlerFile: resolvedFile,
        handlerSymbol,
        metadata: {
          confidence: "inferred",
          routeType: "server"
        }
      });
    }
    return routes;
  }
}
export {
  VaporDetector
};
