"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 amplify_exports = {};
__export(amplify_exports, {
  amplifyResolver: () => amplifyResolver
});
module.exports = __toCommonJS(amplify_exports);
const amplifyResolver = {
  name: "amplify",
  detect(context) {
    if (context.fileExists("amplify/backend.ts") || context.fileExists("amplify/backend.js")) return true;
    const pkg = context.readFile("package.json");
    if (pkg) {
      try {
        const parsed = JSON.parse(pkg);
        const deps = { ...parsed.dependencies, ...parsed.devDependencies };
        if ("@aws-amplify/backend" in deps || "@aws-amplify/backend-cli" in deps) return true;
      } catch {
      }
    }
    if (context.fileExists("amplify/backend/backend-config.json")) return true;
    return false;
  },
  resolve(ref, context) {
    if (ref.referenceName.includes(".") && !ref.referenceName.startsWith(".")) {
      const id = resolveHandlerEntry(ref.referenceName, context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.9, resolvedBy: "framework" };
    }
    if (/^[a-z][a-zA-Z]+$/.test(ref.referenceName)) {
      const id = resolveAmplifyResource(ref.referenceName, context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.8, resolvedBy: "framework" };
    }
    return null;
  },
  extractNodes(filePath, content) {
    const nodes = [];
    const now = Date.now();
    if (!filePath.includes("amplify/")) return nodes;
    const modelPattern = /(\w+)\s*:\s*a\.model\s*\(/g;
    let match;
    while ((match = modelPattern.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const modelName = match[1];
      nodes.push({
        id: `amplify:${filePath}:model:${modelName}:${line}`,
        kind: "class",
        name: modelName,
        qualifiedName: `${filePath}::model.${modelName}`,
        filePath,
        startLine: line,
        endLine: line,
        startColumn: 0,
        endColumn: match[0].length,
        language: "typescript",
        updatedAt: now,
        signature: `a.model("${modelName}")`
      });
    }
    const funcPattern = /defineFunction\s*\(\s*\{[^}]*name\s*:\s*['"]([^'"]+)['"]/g;
    while ((match = funcPattern.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const funcName = match[1];
      nodes.push({
        id: `amplify:${filePath}:function:${funcName}:${line}`,
        kind: "function",
        name: funcName,
        qualifiedName: `${filePath}::function.${funcName}`,
        filePath,
        startLine: line,
        endLine: line,
        startColumn: 0,
        endColumn: match[0].length,
        language: "typescript",
        updatedAt: now,
        signature: `defineFunction("${funcName}")`
      });
    }
    const queryPattern = /a\.(query|mutation)\s*\(\s*['"]([^'"]+)['"]/g;
    while ((match = queryPattern.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const opType = match[1].toUpperCase();
      const opName = match[2];
      nodes.push({
        id: `route:${filePath}:${opType}:${opName}:${line}`,
        kind: "route",
        name: `${opType} ${opName}`,
        qualifiedName: `${filePath}::${opType}.${opName}`,
        filePath,
        startLine: line,
        endLine: line,
        startColumn: 0,
        endColumn: match[0].length,
        language: "typescript",
        updatedAt: now
      });
    }
    const resourcePattern = /(defineAuth|defineStorage|defineBackend|defineData)\s*\(/g;
    while ((match = resourcePattern.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const resourceType = match[1].replace("define", "").toLowerCase();
      nodes.push({
        id: `amplify:${filePath}:resource:${resourceType}:${line}`,
        kind: "component",
        name: resourceType,
        qualifiedName: `${filePath}::resource.${resourceType}`,
        filePath,
        startLine: line,
        endLine: line,
        startColumn: 0,
        endColumn: match[0].length,
        language: "typescript",
        updatedAt: now,
        signature: `${match[1]}()`
      });
    }
    return nodes;
  }
};
function resolveHandlerEntry(entry, context) {
  const cleanPath = entry.replace(/^\.\//, "");
  const extensions = [".ts", ".js", ".mjs"];
  for (const ext of extensions) {
    const candidates = [
      `amplify/${cleanPath}${ext}`,
      `amplify/functions/${cleanPath}${ext}`,
      `${cleanPath}${ext}`
    ];
    for (const candidate of candidates) {
      if (context.fileExists(candidate)) {
        const nodes = context.getNodesInFile(candidate);
        const handler = nodes.find(
          (n) => n.name === "handler" || n.name === "default" || n.kind === "function"
        );
        if (handler) return handler.id;
        if (nodes.length > 0) return nodes[0].id;
      }
    }
  }
  return null;
}
function resolveAmplifyResource(name, context) {
  const candidates = [
    `amplify/${name}/resource.ts`,
    `amplify/${name}/resource.js`,
    `amplify/${name}.ts`
  ];
  for (const candidate of candidates) {
    if (context.fileExists(candidate)) {
      const nodes = context.getNodesInFile(candidate);
      if (nodes.length > 0) return nodes[0].id;
    }
  }
  return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  amplifyResolver
});
//# sourceMappingURL=amplify.js.map
