/**
 * VisibleStateTransferPlanner — orchestrates a full visible-state transfer plan.
 *
 * Delegates key encoding to transferKeys, op-building to transferOps, and shape/aggregation to transferShape.
 *
 * @module domain/services/transfer/VisibleStateTransferPlanner
 */
import { compareStrings, } from "./transferKeys.js";
import { collectNodeShapeDelta, collectEdgeShapeDelta, collectSyncPropertyOps, collectAllContentOps, summarizeOps, } from "./transferShape.js";
export const VISIBLE_STATE_TRANSFER_PLAN_VERSION = 'visible-state-transfer-plan/v1';
/**
 * Assemble shape, property, and content ops into a single ordered operation list.
 *
 * Ordering: add nodes → node properties → node content → add edges → edge properties →
 * edge content → remove edges → remove nodes.
 */
export function assembleOps(parts) {
    return [
        ...parts.nodeShape.addedNodeOps,
        ...parts.nodePropertyOps,
        ...parts.nodeContentOps,
        ...parts.edgeShape.addedEdgeOps,
        ...parts.edgePropertyOps,
        ...parts.edgeContentOps,
        ...parts.edgeShape.removedEdgeOps,
        ...parts.nodeShape.removedNodeOps,
    ];
}
/**
 * Collect all shape, property, and content ops and assemble them into order.
 */
async function buildOps(params) {
    const { sourceReader, targetReader, nodeShape, edgeShape, loaders } = params;
    const syncOps = collectSyncPropertyOps({ sourceReader, targetReader, nodeShape, edgeShape });
    const contentOps = await collectAllContentOps({
        sourceReader, targetReader, nodeShape, edgeShape, loaders,
    });
    return assembleOps({
        nodeShape, edgeShape,
        nodePropertyOps: syncOps.nodePropertyOps,
        edgePropertyOps: syncOps.edgePropertyOps,
        nodeContentOps: contentOps.nodeContentOps,
        edgeContentOps: contentOps.edgeContentOps,
    });
}
/**
 * Produce a complete visible-state transfer plan that transforms target into source.
 */
export async function planVisibleStateTransfer(sourceReader, targetReader, loaders) {
    const nodeShape = collectNodeShapeDelta(sourceReader.getNodes().sort(compareStrings), targetReader.getNodes().sort(compareStrings));
    const edgeShape = collectEdgeShapeDelta(sourceReader, targetReader);
    const ops = await buildOps({ sourceReader, targetReader, nodeShape, edgeShape, loaders });
    return { transferVersion: VISIBLE_STATE_TRANSFER_PLAN_VERSION, ops, summary: summarizeOps(ops) };
}
