import { parseHTML } from "linkedom";
function normalize(s) {
  return s.toLowerCase().replace(/\s+/g, " ").trim();
}
function getSourceText(html) {
  if (!html) return "";
  try {
    const { document: doc } = parseHTML(html);
    const body = doc.body ?? doc.documentElement;
    return body?.textContent ?? "";
  } catch {
    return html.replace(/<[^>]+>/g, " ");
  }
}
function verifyAgainstSource(value, sourceText) {
  if (value === null || value === void 0) return true;
  if (!sourceText) return false;
  return verifyAgainstNormalizedSource(value, normalize(sourceText));
}
function verifyAgainstNormalizedSource(value, haystack) {
  if (value === null || value === void 0) return true;
  if (!haystack) return false;
  if (typeof value === "string") {
    const needle = normalize(value);
    if (!needle) return false;
    return haystack.includes(needle);
  }
  if (typeof value === "number" && Number.isFinite(value)) {
    const asString = String(value);
    const re = new RegExp(`(?:^|[^0-9])${escapeRe(asString)}(?:[^0-9]|$)`);
    return re.test(haystack);
  }
  if (typeof value === "boolean") {
    return haystack.includes(value ? "true" : "false");
  }
  return true;
}
function escapeRe(s) {
  return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function applyEvidenceFilter(input) {
  const out = { ...input.values };
  const rejected = [];
  const haystack = input.sourceText ? normalize(input.sourceText) : "";
  for (const key of input.fields) {
    const v = out[key];
    if (v === null || v === void 0) continue;
    if (!verifyAgainstNormalizedSource(v, haystack)) {
      out[key] = null;
      rejected.push(key);
    }
  }
  return { values: out, rejectedFields: rejected };
}
export {
  applyEvidenceFilter,
  getSourceText,
  verifyAgainstSource
};
//# sourceMappingURL=schema-truth.js.map