/**
 * Stable identity for a doc ref, used to dedupe within a task's docs.
 * Dedup is per-resolution-strategy: an IdDocRef and a SlugDocRef are never
 * treated as the same doc even if they resolve to one (the _id is
 * authoritative; slug/path are informational).
 *
 * Exported so the `--inject-doc` flag-plus-config merge (D0090) dedupes by
 * the exact same key this adapter dedupes by, keeping the two surfaces in
 * lock-step.
 */
export function refKey(ref) {
    if ("id" in ref)
        return `id:${ref.id}`;
    if ("path" in ref)
        return `path:${ref.path}`;
    if ("slug" in ref)
        return `slug:${ref.slug}`;
    return `perspective:${ref.perspective}`;
}
export function injectContextDocsIntoTasks(tasks, injectContextDocs) {
    if (!injectContextDocs || injectContextDocs.length === 0)
        return [...tasks];
    return tasks.map((task) => {
        const existing = task.context?.docs ?? [];
        const present = new Set(existing.map(refKey));
        const additions = [];
        for (const ref of injectContextDocs) {
            const key = refKey(ref);
            if (present.has(key))
                continue;
            present.add(key);
            additions.push(ref);
        }
        if (additions.length === 0)
            return task;
        return {
            ...task,
            context: { ...task.context, docs: [...existing, ...additions] },
        };
    });
}
