const plainCache = new WeakMap();
const withIdCache = new WeakMap();
/**
 * Build the lowercase searchable text for a candidate — title, summary, content, contextual content,
 * labels, references, and metadata. Memoized per candidate object so repeated filter/sort passes
 * over the same list don't rebuild the string on every comparison.
 */
export function candidateText(candidate, options = {}) {
    const cache = options.includeKnowledgeId ? withIdCache : plainCache;
    const hit = cache.get(candidate);
    if (hit !== undefined) {
        return hit;
    }
    const parts = [];
    if (options.includeKnowledgeId) {
        parts.push(candidate.knowledgeId);
    }
    parts.push(candidate.title, candidate.summary, candidate.content, candidate.contextualContent, candidate.labels.map((label) => `${label.type}:${label.value}`).join(' '), candidate.references.map((reference) => reference.uri).join(' '), JSON.stringify(candidate.metadata ?? {}));
    const text = parts.join(' ').toLowerCase();
    cache.set(candidate, text);
    return text;
}
