export async function syncAtomLinks(fromAtomId, links, store, source) {
    const inputs = links.map((link) => ({
        fromAtomId,
        targetKind: link.targetKind ?? 'atom',
        targetAtomId: link.toAtomId,
        relationType: link.kind,
        confidence: link.confidence,
        inferenceSource: source,
    }));
    await store.replaceAtomRelations(fromAtomId, inputs, { source });
    // Merge into atom JSONB links: keep links from OTHER sources, replace this
    // source's slice. We re-read from listAtomRelations to ensure JSONB stays in
    // lockstep with the indexed rows.
    const atom = await store.getAtom(fromAtomId);
    if (!atom)
        return;
    const allRows = await store.listAtomRelations({ fromAtomId, limit: 200 });
    const merged = allRows.map((r) => ({
        toAtomId: r.targetAtomId,
        kind: r.relationType,
        confidence: r.confidence,
    }));
    await store.updateAtom(fromAtomId, { links: merged });
}
