class AspNetDetector {
  name = "aspnet";
  language = "csharp";
  filePattern = /\.cs$/;
  async detect(projectRoot, files) {
    const hasCsproj = files.some((f) => f.endsWith(".csproj") || f.endsWith(".sln"));
    if (hasCsproj) return true;
    return files.some((f) => f.endsWith(".cs"));
  }
  async extractRoutes(filePath, content, ctx) {
    const routes = [];
    if (!content.includes("Controller") && !content.includes("Route") && !content.includes("Http")) {
      return [];
    }
    const classRegex = /\bclass\s+([a-zA-Z0-9_]+)/g;
    const classes = [];
    let classMatch;
    while ((classMatch = classRegex.exec(content)) !== null) {
      const className = classMatch[1];
      const classIndex = classMatch.index;
      const startIdx = Math.max(0, classIndex - 200);
      const preText = content.substring(startIdx, classIndex);
      const routeMatches = [...preText.matchAll(/\[Route\s*\(\s*['"]([^'"]+)['"]\s*\)\]/g)];
      const prefix = routeMatches.length > 0 ? routeMatches[routeMatches.length - 1][1] : "";
      classes.push({
        name: className,
        prefix,
        index: classIndex
      });
    }
    if (classes.length === 0) return [];
    const methodRouteRegex = /((?:\[(?:HttpGet|HttpPost|HttpPut|HttpDelete|HttpPatch|Route)[^\]]*\]\s*)+)[^{;]*\b([a-zA-Z0-9_]+)\s*\(/g;
    let methodMatch;
    while ((methodMatch = methodRouteRegex.exec(content)) !== null) {
      const attributeBlock = methodMatch[1];
      const methodName = methodMatch[2];
      const methodIndex = methodMatch.index;
      const parentClass = classes.filter((c) => c.index < methodIndex).sort((a, b) => b.index - a.index)[0];
      if (!parentClass) continue;
      const attrRegex = /\[(HttpGet|HttpPost|HttpPut|HttpDelete|HttpPatch|Route)(?:\s*\(\s*['"]([^'"]+)['"]\s*\))?[^\]]*\]/g;
      let attrMatch;
      let verb = "GET";
      let methodTemplate = "";
      while ((attrMatch = attrRegex.exec(attributeBlock)) !== null) {
        const attrName = attrMatch[1];
        const templateVal = attrMatch[2] || "";
        if (attrName.startsWith("Http")) {
          verb = attrName.substring(4).toUpperCase();
          if (templateVal) {
            methodTemplate = templateVal;
          }
        } else if (attrName === "Route") {
          if (templateVal) {
            methodTemplate = templateVal;
          }
        }
      }
      const className = parentClass.name;
      const controllerName = className.endsWith("Controller") ? className.substring(0, className.length - 10).toLowerCase() : className.toLowerCase();
      let classRoutePrefix = parentClass.prefix;
      classRoutePrefix = classRoutePrefix.replace(/\[controller\]/gi, controllerName);
      const cleanClassPrefix = classRoutePrefix.replace(/^\/|\/$/g, "");
      const cleanMethodTemplate = methodTemplate.replace(/^\/|\/$/g, "");
      let combinedPath = "/" + [cleanClassPrefix, cleanMethodTemplate].filter(Boolean).join("/");
      combinedPath = combinedPath.replace(/\[action\]/gi, methodName.toLowerCase());
      combinedPath = combinedPath.replace(/\{([a-zA-Z0-9_?*]+)(?::[^}]+)?\}/g, "{$1}");
      routes.push({
        framework: this.name,
        method: verb,
        path: combinedPath,
        handlerFile: filePath,
        handlerSymbol: `${className}.${methodName}`,
        metadata: {
          confidence: "inferred",
          routeType: "server"
        }
      });
    }
    return routes;
  }
}
export {
  AspNetDetector
};
