"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 terraform_exports = {};
__export(terraform_exports, {
  terraformResolver: () => terraformResolver
});
module.exports = __toCommonJS(terraform_exports);
const terraformResolver = {
  name: "terraform",
  detect(context) {
    if (context.fileExists(".terraform") || context.fileExists(".terraform.lock.hcl")) return true;
    return context.getAllFiles().some((f) => f.endsWith(".tf"));
  },
  resolve(ref, context) {
    if (ref.referenceName.includes(".")) {
      const parts = ref.referenceName.split(".");
      if (parts.length >= 2) {
        const resourceName = parts.slice(0, 2).join(".");
        const id = resolveResource(resourceName, context);
        if (id) return { original: ref, targetNodeId: id, confidence: 0.85, resolvedBy: "framework" };
      }
    }
    if (ref.referenceName.startsWith("module.")) {
      const moduleName = ref.referenceName.replace("module.", "");
      const id = resolveModule(moduleName, context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.9, resolvedBy: "framework" };
    }
    if (ref.referenceName.startsWith("var.")) {
      const varName = ref.referenceName.replace("var.", "");
      const id = resolveVariable(varName, context);
      if (id) return { original: ref, targetNodeId: id, confidence: 0.9, resolvedBy: "framework" };
    }
    return null;
  },
  extractNodes(filePath, content) {
    if (!filePath.endsWith(".tf") && !filePath.endsWith(".tfvars")) return [];
    const nodes = [];
    const now = Date.now();
    const resourcePattern = /^resource\s+"([^"]+)"\s+"([^"]+)"\s*\{/gm;
    let match;
    while ((match = resourcePattern.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const resourceType = match[1];
      const resourceName = match[2];
      const name = `${resourceType}.${resourceName}`;
      const endLine = findBlockEnd(content, match.index + match[0].length);
      nodes.push({
        id: `resource:${filePath}:${name}:${line}`,
        kind: "class",
        name,
        qualifiedName: `${filePath}::resource.${name}`,
        filePath,
        startLine: line,
        endLine,
        startColumn: 0,
        endColumn: match[0].length,
        language: "hcl",
        updatedAt: now,
        signature: `resource "${resourceType}" "${resourceName}"`
      });
    }
    const dataPattern = /^data\s+"([^"]+)"\s+"([^"]+)"\s*\{/gm;
    while ((match = dataPattern.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const dataType = match[1];
      const dataName = match[2];
      const name = `${dataType}.${dataName}`;
      const endLine = findBlockEnd(content, match.index + match[0].length);
      nodes.push({
        id: `data:${filePath}:${name}:${line}`,
        kind: "variable",
        name: `data.${name}`,
        qualifiedName: `${filePath}::data.${name}`,
        filePath,
        startLine: line,
        endLine,
        startColumn: 0,
        endColumn: match[0].length,
        language: "hcl",
        updatedAt: now,
        signature: `data "${dataType}" "${dataName}"`
      });
    }
    const modulePattern = /^module\s+"([^"]+)"\s*\{/gm;
    while ((match = modulePattern.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const moduleName = match[1];
      const endLine = findBlockEnd(content, match.index + match[0].length);
      nodes.push({
        id: `module:${filePath}:${moduleName}:${line}`,
        kind: "namespace",
        name: `module.${moduleName}`,
        qualifiedName: `${filePath}::module.${moduleName}`,
        filePath,
        startLine: line,
        endLine,
        startColumn: 0,
        endColumn: match[0].length,
        language: "hcl",
        updatedAt: now,
        signature: `module "${moduleName}"`
      });
    }
    const variablePattern = /^variable\s+"([^"]+)"\s*\{/gm;
    while ((match = variablePattern.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const varName = match[1];
      const endLine = findBlockEnd(content, match.index + match[0].length);
      nodes.push({
        id: `variable:${filePath}:${varName}:${line}`,
        kind: "variable",
        name: `var.${varName}`,
        qualifiedName: `${filePath}::var.${varName}`,
        filePath,
        startLine: line,
        endLine,
        startColumn: 0,
        endColumn: match[0].length,
        language: "hcl",
        updatedAt: now,
        signature: `variable "${varName}"`
      });
    }
    const outputPattern = /^output\s+"([^"]+)"\s*\{/gm;
    while ((match = outputPattern.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const outputName = match[1];
      const endLine = findBlockEnd(content, match.index + match[0].length);
      nodes.push({
        id: `output:${filePath}:${outputName}:${line}`,
        kind: "variable",
        name: `output.${outputName}`,
        qualifiedName: `${filePath}::output.${outputName}`,
        filePath,
        startLine: line,
        endLine,
        startColumn: 0,
        endColumn: match[0].length,
        language: "hcl",
        updatedAt: now,
        signature: `output "${outputName}"`,
        isExported: true
      });
    }
    const localsPattern = /^locals\s*\{/gm;
    while ((match = localsPattern.exec(content)) !== null) {
      const blockStart = match.index + match[0].length;
      const blockContent = extractBlock(content, blockStart);
      const localPattern = /^\s+(\w+)\s*=/gm;
      let localMatch;
      while ((localMatch = localPattern.exec(blockContent)) !== null) {
        const localLine = content.slice(0, blockStart).split("\n").length + blockContent.slice(0, localMatch.index).split("\n").length - 1;
        const localName = localMatch[1];
        nodes.push({
          id: `local:${filePath}:${localName}:${localLine}`,
          kind: "constant",
          name: `local.${localName}`,
          qualifiedName: `${filePath}::local.${localName}`,
          filePath,
          startLine: localLine,
          endLine: localLine,
          startColumn: 0,
          endColumn: localMatch[0].length,
          language: "hcl",
          updatedAt: now
        });
      }
    }
    const apiResourcePattern = /resource\s+"aws_api_gateway_resource"\s+"([^"]+)"[\s\S]*?path_part\s*=\s*"([^"]+)"/g;
    while ((match = apiResourcePattern.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const routePath = `/${match[2]}`;
      const name = `RESOURCE ${routePath}`;
      nodes.push({
        id: `route:${filePath}:RESOURCE:${routePath}:${line}`,
        kind: "route",
        name,
        qualifiedName: `${filePath}::${name}`,
        filePath,
        startLine: line,
        endLine: line,
        startColumn: 0,
        endColumn: match[0].length,
        language: "hcl",
        updatedAt: now
      });
    }
    const apiMethodPattern = /resource\s+"aws_api_gateway_method"\s+"([^"]+)"[\s\S]*?http_method\s*=\s*"([^"]+)"/g;
    while ((match = apiMethodPattern.exec(content)) !== null) {
      const line = content.slice(0, match.index).split("\n").length;
      const method = match[2];
      const name = `${method} (api_gateway_method.${match[1]})`;
      nodes.push({
        id: `route:${filePath}:${method}:${match[1]}:${line}`,
        kind: "route",
        name,
        qualifiedName: `${filePath}::${name}`,
        filePath,
        startLine: line,
        endLine: line,
        startColumn: 0,
        endColumn: match[0].length,
        language: "hcl",
        updatedAt: now
      });
    }
    return nodes;
  }
};
function findBlockEnd(content, startOffset) {
  let depth = 1;
  for (let i = startOffset; i < content.length; i++) {
    if (content[i] === "{") depth++;
    else if (content[i] === "}") {
      depth--;
      if (depth === 0) return content.slice(0, i).split("\n").length;
    }
  }
  return content.slice(0, startOffset).split("\n").length;
}
function extractBlock(content, startOffset) {
  let depth = 1;
  for (let i = startOffset; i < content.length; i++) {
    if (content[i] === "{") depth++;
    else if (content[i] === "}") {
      depth--;
      if (depth === 0) return content.slice(startOffset, i);
    }
  }
  return content.slice(startOffset);
}
function resolveResource(name, context) {
  for (const file of context.getAllFiles()) {
    if (!file.endsWith(".tf")) continue;
    const node = context.getNodesInFile(file).find((n) => n.name === name);
    if (node) return node.id;
  }
  return null;
}
function resolveModule(name, context) {
  const fullName = `module.${name}`;
  for (const file of context.getAllFiles()) {
    if (!file.endsWith(".tf")) continue;
    const node = context.getNodesInFile(file).find((n) => n.name === fullName);
    if (node) return node.id;
  }
  return null;
}
function resolveVariable(name, context) {
  const fullName = `var.${name}`;
  for (const file of context.getAllFiles()) {
    if (!file.endsWith(".tf")) continue;
    const node = context.getNodesInFile(file).find((n) => n.name === fullName);
    if (node) return node.id;
  }
  return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  terraformResolver
});
//# sourceMappingURL=terraform.js.map
