"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var lint_exports = {};
__export(lint_exports, {
  docsLint: () => docsLint
});
module.exports = __toCommonJS(lint_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var crypto = __toESM(require("crypto"));
function docsLint(db, projectRoot) {
  const result = {
    brokenRefs: [],
    staleSections: [],
    ftsDesync: null,
    orphanRefs: 0,
    totalIssues: 0
  };
  const refs = db.all(`
    SELECT r.section_id, r.qualified_name
    FROM doc_code_refs r
    LEFT JOIN nodes n ON n.qualified_name = r.qualified_name
    WHERE n.id IS NULL
  `);
  result.brokenRefs = refs.map((r) => ({ sectionId: r.section_id, qualifiedName: r.qualified_name }));
  const sections = db.all(`
    SELECT id, file_path, byte_start, byte_end, content_hash
    FROM doc_sections
  `);
  for (const section of sections) {
    const absPath = path.join(projectRoot, section.file_path);
    try {
      if (!fs.existsSync(absPath)) {
        result.staleSections.push({ id: section.id, filePath: section.file_path });
        continue;
      }
      const buffer = fs.readFileSync(absPath);
      const content = buffer.slice(section.byte_start, section.byte_end).toString("utf8");
      const hash = crypto.createHash("sha256").update(content).digest("hex");
      if (hash !== section.content_hash) {
        result.staleSections.push({ id: section.id, filePath: section.file_path });
      }
    } catch {
      result.staleSections.push({ id: section.id, filePath: section.file_path });
    }
  }
  const sectionCount = db.get("SELECT COUNT(*) as cnt FROM doc_sections")?.cnt ?? 0;
  const ftsCount = db.get("SELECT COUNT(*) as cnt FROM doc_sections_fts")?.cnt ?? 0;
  if (sectionCount !== ftsCount) {
    result.ftsDesync = { sections: sectionCount, ftsRows: ftsCount };
  }
  const orphans = db.get(`
    SELECT COUNT(*) as cnt
    FROM doc_code_refs r
    LEFT JOIN doc_sections s ON s.id = r.section_id
    WHERE s.id IS NULL
  `)?.cnt ?? 0;
  result.orphanRefs = orphans;
  result.totalIssues = result.brokenRefs.length + result.staleSections.length + (result.ftsDesync ? 1 : 0) + result.orphanRefs;
  return result;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  docsLint
});
//# sourceMappingURL=lint.js.map
