/** Build a content relationship graph from a document's data and field definitions. */
export function buildContentGraph(documentId, collection, data, fieldTypes) {
    const nodes = [{ documentId, collection }];
    const edges = [];
    for (const [fieldName, fieldDef] of Object.entries(fieldTypes)) {
        const value = data[fieldName];
        if (!value)
            continue;
        if (fieldDef.type === 'relationship') {
            const ids = Array.isArray(value) ? value : [value];
            for (const targetId of ids) {
                if (typeof targetId === 'string') {
                    edges.push({ from: documentId, to: targetId, fieldName, type: 'relationship' });
                    nodes.push({ documentId: targetId, collection: fieldDef.relationTo ?? 'unknown' });
                }
            }
        }
        if (fieldDef.type === 'media') {
            const ids = Array.isArray(value) ? value : [value];
            for (const targetId of ids) {
                if (typeof targetId === 'string') {
                    edges.push({ from: documentId, to: targetId, fieldName, type: 'media' });
                }
            }
        }
    }
    return { nodes, edges };
}
/** Serialize a content graph to the JSON format stored in the document. */
export function serializeGraph(graph) {
    return {
        nodes: graph.nodes,
        edges: graph.edges,
    };
}
/** Find all documents that reference a given document. */
export function findIncomingReferences(targetId, graphs) {
    return graphs.flatMap((g) => g.edges.filter((e) => e.to === targetId));
}
//# sourceMappingURL=content-graph.js.map