"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var svelte_exports = {};
__export(svelte_exports, {
  svelteResolver: () => svelteResolver
});
module.exports = __toCommonJS(svelte_exports);
const SVELTE_RUNES = /* @__PURE__ */ new Set([
  "$state",
  "$state.raw",
  "$state.snapshot",
  "$derived",
  "$derived.by",
  "$effect",
  "$effect.pre",
  "$effect.root",
  "$effect.tracking",
  "$props",
  "$bindable",
  "$inspect",
  "$host"
]);
const SVELTEKIT_MODULE_PREFIXES = [
  "$app/navigation",
  "$app/stores",
  "$app/environment",
  "$app/forms",
  "$app/paths",
  "$env/static/private",
  "$env/static/public",
  "$env/dynamic/private",
  "$env/dynamic/public"
];
const svelteResolver = {
  name: "svelte",
  detect(context) {
    const packageJson = context.readFile("package.json");
    if (packageJson) {
      try {
        const pkg = JSON.parse(packageJson);
        const deps = { ...pkg.dependencies, ...pkg.devDependencies };
        if (deps.svelte || deps["@sveltejs/kit"]) return true;
      } catch {
      }
    }
    return context.getAllFiles().some((f) => f.endsWith(".svelte"));
  },
  resolve(ref, context) {
    if (SVELTE_RUNES.has(ref.referenceName)) {
      return { original: ref, targetNodeId: ref.fromNodeId, confidence: 1, resolvedBy: "framework" };
    }
    if (ref.referenceName.startsWith("$") && !ref.referenceName.startsWith("$$")) {
      const storeName = ref.referenceName.substring(1);
      const node = context.getNodesByName(storeName).find(
        (n) => n.kind === "variable" || n.kind === "constant"
      );
      if (node) return { original: ref, targetNodeId: node.id, confidence: 0.85, resolvedBy: "framework" };
    }
    if (ref.referenceKind === "imports" && ref.referenceName.startsWith("$")) {
      if (ref.referenceName.startsWith("$lib/")) {
        const libPath = ref.referenceName.replace("$lib/", "src/lib/");
        for (const ext of ["", ".ts", ".js", ".svelte", "/index.ts", "/index.js"]) {
          const fullPath = libPath + ext;
          if (context.fileExists(fullPath)) {
            const nodes = context.getNodesInFile(fullPath);
            if (nodes.length > 0) return { original: ref, targetNodeId: nodes[0].id, confidence: 0.9, resolvedBy: "framework" };
          }
        }
      }
      if (SVELTEKIT_MODULE_PREFIXES.some((p) => ref.referenceName.startsWith(p))) {
        return { original: ref, targetNodeId: ref.fromNodeId, confidence: 1, resolvedBy: "framework" };
      }
    }
    if (/^[A-Z][a-zA-Z0-9]*$/.test(ref.referenceName) && ref.referenceKind === "calls") {
      const id = resolveComponent(ref.referenceName, ref.filePath, context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.8, resolvedBy: "framework" };
    }
    return null;
  },
  extractNodes(filePath, _content) {
    const nodes = [];
    const now = Date.now();
    const fileName = filePath.split(/[/\\]/).pop() || "";
    const routeMatch = SVELTEKIT_ROUTE_FILES[fileName];
    if (routeMatch) {
      const routePath = filePathToSvelteKitRoute(filePath);
      if (routePath) {
        nodes.push({
          id: `route:${filePath}:${routePath}:1`,
          kind: "route",
          name: routePath,
          qualifiedName: `${filePath}::route:${routePath}`,
          filePath,
          startLine: 1,
          endLine: 1,
          startColumn: 0,
          endColumn: 0,
          language: filePath.endsWith(".svelte") ? "svelte" : "typescript",
          updatedAt: now
        });
      }
    }
    return nodes;
  }
};
const SVELTEKIT_ROUTE_FILES = {
  "+page.svelte": "page",
  "+page.ts": "page-load",
  "+page.js": "page-load",
  "+page.server.ts": "page-server-load",
  "+page.server.js": "page-server-load",
  "+layout.svelte": "layout",
  "+layout.ts": "layout-load",
  "+layout.js": "layout-load",
  "+layout.server.ts": "layout-server-load",
  "+layout.server.js": "layout-server-load",
  "+server.ts": "api-endpoint",
  "+server.js": "api-endpoint",
  "+error.svelte": "error-page"
};
function resolveComponent(name, fromFile, context) {
  const allFiles = context.getAllFiles();
  const svelteFiles = allFiles.filter((f) => f.endsWith(".svelte"));
  for (const file of svelteFiles) {
    const fileName = file.split(/[/\\]/).pop() || "";
    if (fileName.replace(/\.svelte$/, "") === name) {
      const node = context.getNodesInFile(file).find((n) => n.kind === "component" && n.name === name);
      if (node) return node.id;
    }
  }
  const fromDir = fromFile.substring(0, fromFile.lastIndexOf("/"));
  for (const file of svelteFiles) {
    if (file.startsWith(fromDir)) {
      const fileName = file.split(/[/\\]/).pop() || "";
      if (fileName.replace(/\.svelte$/, "") === name) {
        const node = context.getNodesInFile(file).find((n) => n.kind === "component");
        if (node) return node.id;
      }
    }
  }
  return null;
}
function filePathToSvelteKitRoute(filePath) {
  const normalized = filePath.replace(/\\/g, "/");
  const routesIndex = normalized.indexOf("/routes/");
  if (routesIndex === -1) return null;
  const afterRoutes = normalized.substring(routesIndex + "/routes/".length);
  const lastSlash = afterRoutes.lastIndexOf("/");
  const dirPath = lastSlash === -1 ? "" : afterRoutes.substring(0, lastSlash);
  let route = "/" + dirPath.replace(/\[\.\.\.([^\]]+)\]/g, "*$1").replace(/\[{2}([^\]]+)\]{2}/g, ":$1?").replace(/\[([^\]]+)\]/g, ":$1");
  if (route === "/") return "/";
  return route.replace(/\/$/, "");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  svelteResolver
});
//# sourceMappingURL=svelte.js.map
