// src/delinearize.ts
var PRONOUN_MAP = {
  "he": { types: ["person"], possessive: false, group: "masc" },
  "him": { types: ["person"], possessive: false, group: "masc" },
  "his": { types: ["person"], possessive: true, group: "masc" },
  "she": { types: ["person"], possessive: false, group: "fem" },
  // "her" omitted — ambiguous between object ("saw her") and possessive ("her stack")
  "they": { types: ["company", "project", "other"], possessive: false, group: "they" },
  "them": { types: ["company", "project", "other"], possessive: false, group: "they" },
  "their": { types: ["company", "project", "other"], possessive: true, group: "they" },
  "it": { types: ["project", "tool", "company"], possessive: false, group: "it" },
  "its": { types: ["project", "tool", "company"], possessive: true, group: "it" }
};
function resolveCoReferences(fact, entities) {
  if (entities.length === 0) return fact;
  const matchingEntries = [];
  const typeToGroups = /* @__PURE__ */ new Map();
  for (const [pronoun, info] of Object.entries(PRONOUN_MAP)) {
    const regex = new RegExp(`\\b${pronoun}\\b(?![''\u2019])`, "gi");
    if (!regex.test(fact)) continue;
    const candidates = entities.filter((e) => info.types.includes(e.type));
    if (candidates.length !== 1) continue;
    matchingEntries.push({ pronoun, info, entity: candidates[0] });
    const matchedType = candidates[0].type;
    if (!typeToGroups.has(matchedType)) typeToGroups.set(matchedType, /* @__PURE__ */ new Set());
    typeToGroups.get(matchedType).add(info.group);
  }
  const ambiguousTypes = /* @__PURE__ */ new Set();
  for (const [type, groups] of typeToGroups) {
    const matchedEntities = entities.filter((e) => e.type === type);
    if (matchedEntities.length === 1 && groups.size > 1) {
      ambiguousTypes.add(type);
    }
  }
  const safeEntries = matchingEntries.filter(
    (e) => !e.info.types.some((t) => ambiguousTypes.has(t))
  );
  if (safeEntries.length === 0) return fact;
  let result = fact;
  const placeholders = [];
  for (const { pronoun, info, entity } of safeEntries) {
    const placeholder = `\0ENTITY_${placeholders.length}\0`;
    const safeEntityName = entity.name.replace(/\$/g, "$$$$");
    const replacement = info.possessive ? `${safeEntityName}'s` : safeEntityName;
    placeholders.push(replacement);
    result = result.replace(
      new RegExp(`\\b${pronoun}\\b(?![''\u2019])`, "gi"),
      placeholder
    );
  }
  for (let i = 0; i < placeholders.length; i++) {
    result = result.replaceAll(`\0ENTITY_${i}\0`, placeholders[i]);
  }
  return result;
}
function formatDate(d) {
  return d.toISOString().slice(0, 10);
}
var TEMPORAL_PATTERNS = [
  {
    pattern: /\bearlier today\b/gi,
    replace: (now) => `earlier on ${formatDate(now)}`
  },
  {
    pattern: /\bthis morning\b/gi,
    replace: (now) => `on the morning of ${formatDate(now)}`
  },
  {
    pattern: /\bthis afternoon\b/gi,
    replace: (now) => `on the afternoon of ${formatDate(now)}`
  },
  {
    pattern: /\bthis evening\b/gi,
    replace: (now) => `on the evening of ${formatDate(now)}`
  },
  {
    pattern: /\blast week\b/gi,
    replace: (now) => {
      const d = new Date(now);
      d.setUTCDate(d.getUTCDate() - 7);
      return `around ${formatDate(d)}`;
    }
  },
  {
    pattern: /\blast month\b/gi,
    replace: (now) => {
      const d = new Date(now);
      d.setUTCDate(1);
      d.setUTCMonth(d.getUTCMonth() - 1);
      return `around ${formatDate(d)}`;
    }
  },
  // Simple patterns last
  {
    pattern: /\byesterday\b/gi,
    replace: (now) => {
      const d = new Date(now);
      d.setUTCDate(d.getUTCDate() - 1);
      return `on ${formatDate(d)}`;
    }
  },
  {
    pattern: /\btoday\b/gi,
    replace: (now) => `on ${formatDate(now)}`
  },
  {
    pattern: /\btomorrow\b/gi,
    replace: (now) => {
      const d = new Date(now);
      d.setUTCDate(d.getUTCDate() + 1);
      return `on ${formatDate(d)}`;
    }
  }
];
function anchorTemporalExpressions(fact, now) {
  let result = fact;
  for (const { pattern, replace } of TEMPORAL_PATTERNS) {
    result = result.replace(pattern, replace(now));
  }
  return result;
}
function delinearize(factContent, entities, timestamp) {
  let result = anchorTemporalExpressions(factContent, timestamp);
  result = resolveCoReferences(result, entities);
  return result;
}

export {
  resolveCoReferences,
  anchorTemporalExpressions,
  delinearize
};
//# sourceMappingURL=chunk-VEWZZM3H.js.map