import { join } from "node:path";
import { existsSync } from "node:fs";
import { hasPackageJsonDependency } from "../utils.js";
class SvelteKitDetector {
  name = "sveltekit";
  language = "typescript";
  filePattern = /\.(svelte|ts|js)$/;
  async detect(projectRoot, files) {
    if (files.some((f) => f === "svelte.config.js" || f.endsWith("/svelte.config.js"))) {
      return true;
    }
    const svelteConfigPath = join(projectRoot, "svelte.config.js");
    if (existsSync(svelteConfigPath)) {
      return true;
    }
    return hasPackageJsonDependency(projectRoot, files, ["@sveltejs/kit", "svelte"]);
  }
  async extractRoutes(filePath, content, ctx) {
    const routes = [];
    const pathNormalized = filePath.replace(/\\/g, "/");
    if (pathNormalized.includes("src/routes/")) {
      const routesIndex = pathNormalized.indexOf("src/routes/");
      let routePart = pathNormalized.substring(routesIndex + 11);
      if (routePart.endsWith("+page.svelte")) {
        routePart = routePart.replace(/\/\+page\.svelte$/, "");
        let routePath = "/" + routePart;
        if (routePath === "/+") routePath = "/";
        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 (routePart.endsWith("+server.ts") || routePart.endsWith("+server.js")) {
        routePart = routePart.replace(/\/\+server\.[a-zA-Z0-9]+$/, "");
        let routePath = "/" + routePart;
        if (routePath === "/+") routePath = "/";
        if (routePath === "") routePath = "/";
        routePath = routePath.replace(/\[([a-zA-Z0-9_]+)\]/g, "{$1}");
        const verbRegex = /export\s+(?:const|async\s+function)\s+(GET|POST|PUT|DELETE|PATCH)\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 (routePart.endsWith("+page.server.ts") || routePart.endsWith("+page.server.js")) {
        routePart = routePart.replace(/\/\+page\.server\.[a-zA-Z0-9]+$/, "");
        let routePath = "/" + routePart;
        if (routePath === "/+") routePath = "/";
        if (routePath === "") routePath = "/";
        routePath = routePath.replace(/\[([a-zA-Z0-9_]+)\]/g, "{$1}");
        const actionsIndex = content.indexOf("export const actions");
        if (actionsIndex !== -1) {
          const braceStart = content.indexOf("{", actionsIndex);
          if (braceStart !== -1) {
            const bodyObj = getBracedContent(content, braceStart);
            if (bodyObj) {
              const actionKeysRegex = /\b([a-zA-Z0-9_]+)\s*:/g;
              let keyMatch;
              while ((keyMatch = actionKeysRegex.exec(bodyObj.content)) !== null) {
                const actionName = keyMatch[1];
                routes.push({
                  framework: this.name,
                  method: "POST",
                  path: actionName === "default" ? routePath : `${routePath}?/${actionName}`,
                  handlerFile: filePath,
                  handlerSymbol: `actions.${actionName}`,
                  metadata: {
                    confidence: "inferred",
                    routeType: "server"
                  }
                });
              }
            }
          }
        }
      }
    }
    return routes;
  }
}
function getBracedContent(str, startIndex) {
  let depth = 0;
  let inString = false;
  let stringChar = "";
  let escape = false;
  for (let i = startIndex; i < str.length; i++) {
    const char = str[i];
    if (escape) {
      escape = false;
      continue;
    }
    if (char === "\\") {
      escape = true;
      continue;
    }
    if (inString) {
      if (char === stringChar) {
        inString = false;
      }
      continue;
    }
    if (char === "'" || char === '"' || char === "`") {
      inString = true;
      stringChar = char;
      continue;
    }
    if (char === "{") {
      depth++;
      if (depth === 1) {
        startIndex = i + 1;
      }
    } else if (char === "}") {
      depth--;
      if (depth === 0) {
        return {
          content: str.substring(startIndex, i),
          endIndex: i + 1
        };
      }
    }
  }
  return null;
}
export {
  SvelteKitDetector
};
