"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 elixir_exports = {};
__export(elixir_exports, {
  phoenixResolver: () => phoenixResolver
});
module.exports = __toCommonJS(elixir_exports);
const phoenixResolver = {
  name: "phoenix",
  detect(context) {
    const mixExs = context.readFile("mix.exs");
    if (mixExs && mixExs.includes(":phoenix")) return true;
    return context.fileExists("lib") && (context.fileExists("config/config.exs") || context.fileExists("mix.exs"));
  },
  resolve(ref, context) {
    if (ref.referenceName.endsWith("Controller")) {
      const id = resolveModule(ref.referenceName, "controllers", context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.85, resolvedBy: "framework" };
    }
    if (ref.referenceName.endsWith("Live") || ref.referenceName.endsWith("LiveView")) {
      const id = resolveModule(ref.referenceName, "live", context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.85, resolvedBy: "framework" };
    }
    if (ref.referenceName.endsWith("Channel")) {
      const id = resolveModule(ref.referenceName, "channels", context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.85, resolvedBy: "framework" };
    }
    if (/^[A-Z]/.test(ref.referenceName)) {
      const id = resolveModule(ref.referenceName, "lib", context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.7, resolvedBy: "framework" };
    }
    return null;
  },
  extractNodes(filePath, content) {
    const nodes = [];
    const now = Date.now();
    if (!filePath.includes("router.ex") && !filePath.includes("router.exs")) return nodes;
    const httpMethods = /(get|post|put|patch|delete)\s+"([^"]+)"/g;
    let match;
    while ((match = httpMethods.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const [, method, routePath] = match;
      const name = `${method.toUpperCase()} ${routePath}`;
      nodes.push({
        id: `route:${filePath}:${method.toUpperCase()}:${routePath}:${line}`,
        kind: "route",
        name,
        qualifiedName: `${filePath}::${name}`,
        filePath,
        startLine: line,
        endLine: line,
        startColumn: 0,
        endColumn: match[0].length,
        language: "elixir",
        updatedAt: now
      });
    }
    const resources = /resources\s+"([^"]+)"/g;
    while ((match = resources.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const [, routePath] = match;
      const name = `resources ${routePath}`;
      nodes.push({
        id: `route:${filePath}:resources:${routePath}:${line}`,
        kind: "route",
        name,
        qualifiedName: `${filePath}::${name}`,
        filePath,
        startLine: line,
        endLine: line,
        startColumn: 0,
        endColumn: match[0].length,
        language: "elixir",
        updatedAt: now
      });
    }
    const liveRoutes = /live\s+"([^"]+)"/g;
    while ((match = liveRoutes.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const [, routePath] = match;
      const name = `live ${routePath}`;
      nodes.push({
        id: `route:${filePath}:live:${routePath}:${line}`,
        kind: "route",
        name,
        qualifiedName: `${filePath}::${name}`,
        filePath,
        startLine: line,
        endLine: line,
        startColumn: 0,
        endColumn: match[0].length,
        language: "elixir",
        updatedAt: now
      });
    }
    return nodes;
  }
};
function resolveModule(name, subdir, context) {
  const snake = toSnake(name);
  const candidates = [
    `lib/${subdir}/${snake}.ex`,
    `lib/${snake}.ex`
  ];
  for (const p of candidates) {
    if (context.fileExists(p)) {
      const node = context.getNodesInFile(p).find((n) => n.kind === "module" && n.name === name);
      if (node) return node.id;
    }
  }
  for (const file of context.getAllFiles()) {
    if (file.endsWith(".ex") || file.endsWith(".exs")) {
      const node = context.getNodesInFile(file).find((n) => n.kind === "module" && n.name === name);
      if (node) return node.id;
    }
  }
  return null;
}
function toSnake(name) {
  return name.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  phoenixResolver
});
//# sourceMappingURL=elixir.js.map
