"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 linker_exports = {};
__export(linker_exports, {
  extractCandidateRefs: () => extractCandidateRefs,
  linkSection: () => linkSection,
  resolveCodeRefs: () => resolveCodeRefs
});
module.exports = __toCommonJS(linker_exports);
const BACKTICK_RE = /`([^`\n]+)`/g;
const IDENTIFIER_RE = /\b([A-Z][a-zA-Z0-9]+(?:\.[a-zA-Z][a-zA-Z0-9]*)?)\b/g;
const SNAKE_CASE_RE = /\b([a-z][a-z0-9]*(?:_[a-z0-9]+)+)\b/g;
const IMPORT_PATH_RE = /(?:from|import)\s+['"]([^'"]+)['"]/g;
const IGNORE_WORDS = /* @__PURE__ */ new Set([
  "TODO",
  "NOTE",
  "FIXME",
  "HACK",
  "WARNING",
  "IMPORTANT",
  "DEPRECATED",
  "README",
  "LICENSE",
  "CHANGELOG",
  "CONTRIBUTING",
  "HTTP",
  "HTTPS",
  "JSON",
  "HTML",
  "CSS",
  "SQL",
  "API",
  "REST",
  "CRUD",
  "GET",
  "POST",
  "PUT",
  "DELETE",
  "PATCH",
  "HEAD",
  "OPTIONS",
  "String",
  "Number",
  "Boolean",
  "Object",
  "Array",
  "Function",
  "Promise",
  "Error",
  "Date",
  "Map",
  "Set",
  "RegExp",
  "Buffer",
  "Symbol",
  "True",
  "False",
  "None",
  "Null",
  "Undefined",
  "This",
  "Self",
  "Super",
  "Class",
  "Interface",
  "Type"
]);
function extractCandidateRefs(content) {
  const candidates = /* @__PURE__ */ new Set();
  let match;
  BACKTICK_RE.lastIndex = 0;
  while ((match = BACKTICK_RE.exec(content)) !== null) {
    const ref = match[1].trim();
    if (ref.includes(" ") || ref.startsWith("/") || ref.startsWith("http") || ref.startsWith("$")) continue;
    if (ref.includes(".")) {
      const parts = ref.split(".");
      for (const part of parts) {
        if (part.length >= 2 && !IGNORE_WORDS.has(part)) candidates.add(part);
      }
    }
    if (ref.length >= 2 && !IGNORE_WORDS.has(ref)) candidates.add(ref);
  }
  IDENTIFIER_RE.lastIndex = 0;
  while ((match = IDENTIFIER_RE.exec(content)) !== null) {
    const ref = match[1];
    if (!IGNORE_WORDS.has(ref) && ref.length >= 3) {
      candidates.add(ref);
    }
  }
  SNAKE_CASE_RE.lastIndex = 0;
  while ((match = SNAKE_CASE_RE.exec(content)) !== null) {
    const ref = match[1];
    if (ref.length >= 4) candidates.add(ref);
  }
  return [...candidates];
}
function resolveCodeRefs(sectionId, candidates, db) {
  if (candidates.length === 0) return [];
  const refs = [];
  const seen = /* @__PURE__ */ new Set();
  const CHUNK_SIZE = 50;
  for (let i = 0; i < candidates.length; i += CHUNK_SIZE) {
    const chunk = candidates.slice(i, i + CHUNK_SIZE);
    const placeholders = chunk.map(() => "?").join(",");
    const rows = db.all(
      `SELECT name, qualified_name FROM nodes WHERE name IN (${placeholders}) GROUP BY qualified_name`,
      chunk
    );
    for (const row of rows) {
      const key = `${sectionId}:${row.qualified_name}`;
      if (seen.has(key)) continue;
      seen.add(key);
      const refType = "mentions";
      const confidence = chunk.includes(row.name) ? 0.8 : 0.6;
      refs.push({
        sectionId,
        qualifiedName: row.qualified_name,
        refType,
        confidence
      });
    }
  }
  return refs;
}
function linkSection(sectionId, content, db) {
  const candidates = extractCandidateRefs(content);
  return resolveCodeRefs(sectionId, candidates, db);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  extractCandidateRefs,
  linkSection,
  resolveCodeRefs
});
//# sourceMappingURL=linker.js.map
