/**
 * WarpGraph — the public API surface for git-warp.
 *
 * WARP is a recursive, witnessed admission architecture over bounded
 * frontier-relative causal sites. The admission kernel recurs at three
 * scales: local tick admission, braid-local admission, and distributed
 * suffix admission. The stack decomposes into three moments:
 *
 *   Commitment — plural claims are admitted into frontier-relative truth
 *   Folding    — admitted history is re-expressed in boundary-equivalent form
 *   Revelation — admitted history is exposed under bounded rights
 *
 * `openWarpGraph()` is the advanced composition root. It accepts the governing
 * policy, witness infrastructure, and revelation regime as typed ports,
 * wires controllers, and returns a frozen capability bag organized by
 * architectural moment.
 */
import WarpError from "./errors/WarpError.js";
import { openWarpGraphRuntime } from "./warp/WarpGraphRuntimeBridge.js";
function requireCapabilityMethod(owner, capabilityName, methodName) {
    if (typeof Reflect.get(owner, methodName) === 'function') {
        return;
    }
    throw new WarpError(`${capabilityName} is missing required method: ${methodName}()`, `E_WARPGRAPH_${capabilityName.toUpperCase()}_MISSING_METHOD`);
}
function requireCapability(owner, capabilityName, methodNames) {
    for (const methodName of methodNames) {
        requireCapabilityMethod(owner, capabilityName, methodName);
    }
}
function bindQueryCapability(runtime) {
    requireCapability(runtime, 'query', [
        'hasNode', 'getNodeProps', 'getEdgeProps', 'neighbors',
        'getStateSnapshot', 'getNodes', 'getEdges', 'getPropertyCount',
        'query', 'worldline', 'observer', 'translationCost',
        'getContentOid', 'getContentMeta', 'getContent',
        'getEdgeContentOid', 'getEdgeContentMeta', 'getEdgeContent',
        'getContentStream', 'getEdgeContentStream',
    ]);
    return Object.freeze({
        hasNode: runtime.hasNode.bind(runtime),
        getNodeProps: runtime.getNodeProps.bind(runtime),
        getEdgeProps: runtime.getEdgeProps.bind(runtime),
        neighbors: runtime.neighbors.bind(runtime),
        getStateSnapshot: runtime.getStateSnapshot.bind(runtime),
        getNodes: runtime.getNodes.bind(runtime),
        getEdges: runtime.getEdges.bind(runtime),
        getPropertyCount: runtime.getPropertyCount.bind(runtime),
        query: runtime.query.bind(runtime),
        worldline: runtime.worldline.bind(runtime),
        observer: runtime.observer.bind(runtime),
        translationCost: runtime.translationCost.bind(runtime),
        getContentOid: runtime.getContentOid.bind(runtime),
        getContentMeta: runtime.getContentMeta.bind(runtime),
        getContent: runtime.getContent.bind(runtime),
        getEdgeContentOid: runtime.getEdgeContentOid.bind(runtime),
        getEdgeContentMeta: runtime.getEdgeContentMeta.bind(runtime),
        getEdgeContent: runtime.getEdgeContent.bind(runtime),
        getContentStream: runtime.getContentStream.bind(runtime),
        getEdgeContentStream: runtime.getEdgeContentStream.bind(runtime),
    });
}
function bindPatchCapability(runtime) {
    requireCapability(runtime, 'patch', [
        'createPatch', 'patch', 'patchMany', 'getWriterPatches',
        'writer', 'discoverWriters', 'discoverTicks', 'join',
    ]);
    return Object.freeze({
        createPatch: runtime.createPatch.bind(runtime),
        patch: runtime.patch.bind(runtime),
        patchMany: runtime.patchMany.bind(runtime),
        getWriterPatches: runtime.getWriterPatches.bind(runtime),
        writer: runtime.writer.bind(runtime),
        discoverWriters: runtime.discoverWriters.bind(runtime),
        discoverTicks: runtime.discoverTicks.bind(runtime),
        join: runtime.join.bind(runtime),
    });
}
function bindSyncCapability(runtime) {
    const syncSurface = runtime;
    requireCapability(runtime, 'sync', [
        'getFrontier', 'hasFrontierChanged', 'status', 'createSyncRequest',
        'processSyncRequest', 'applySyncResponse', 'syncNeeded',
        'syncWith', 'serve',
    ]);
    return Object.freeze({
        getFrontier: syncSurface.getFrontier.bind(runtime),
        hasFrontierChanged: syncSurface.hasFrontierChanged.bind(runtime),
        status: syncSurface.status.bind(runtime),
        createSyncRequest: syncSurface.createSyncRequest.bind(runtime),
        processSyncRequest: syncSurface.processSyncRequest.bind(runtime),
        applySyncResponse: syncSurface.applySyncResponse.bind(runtime),
        syncNeeded: syncSurface.syncNeeded.bind(runtime),
        async syncWith(remote, options) {
            return await syncSurface.syncWith.call(runtime, remote, options);
        },
        serve: syncSurface.serve.bind(runtime),
    });
}
function bindStrandCapability(runtime) {
    requireCapability(runtime, 'strand', [
        'createStrand', 'braidStrand', 'getStrand', 'listStrands', 'dropStrand',
        'materializeStrand', 'getStrandPatches', 'patchesForStrand',
        'createStrandPatch', 'patchStrand', 'queueStrandIntent',
        'listStrandIntents', 'tickStrand', 'analyzeConflicts',
    ]);
    return Object.freeze({
        createStrand: runtime.createStrand.bind(runtime),
        braidStrand: runtime.braidStrand.bind(runtime),
        getStrand: runtime.getStrand.bind(runtime),
        listStrands: runtime.listStrands.bind(runtime),
        dropStrand: runtime.dropStrand.bind(runtime),
        materializeStrand: runtime.materializeStrand.bind(runtime),
        getStrandPatches: runtime.getStrandPatches.bind(runtime),
        patchesForStrand: runtime.patchesForStrand.bind(runtime),
        createStrandPatch: runtime.createStrandPatch.bind(runtime),
        patchStrand: runtime.patchStrand.bind(runtime),
        queueStrandIntent: runtime.queueStrandIntent.bind(runtime),
        listStrandIntents: runtime.listStrandIntents.bind(runtime),
        tickStrand: runtime.tickStrand.bind(runtime),
        analyzeConflicts: runtime.analyzeConflicts.bind(runtime),
    });
}
function bindCheckpointCapability(runtime) {
    requireCapability(runtime, 'checkpoint', [
        'createCheckpoint', 'syncCoverage', 'maybeRunGC', 'runGC', 'getGCMetrics',
    ]);
    return Object.freeze({
        createCheckpoint: runtime.createCheckpoint.bind(runtime),
        syncCoverage: runtime.syncCoverage.bind(runtime),
        maybeRunGC: runtime.maybeRunGC.bind(runtime),
        runGC: runtime.runGC.bind(runtime),
        getGCMetrics: runtime.getGCMetrics.bind(runtime),
    });
}
function bindProvenanceCapability(runtime) {
    requireCapability(runtime, 'provenance', [
        'patchesFor', 'materializeSlice', 'loadPatchBySha',
    ]);
    return Object.freeze({
        patchesFor: runtime.patchesFor.bind(runtime),
        materializeSlice: runtime.materializeSlice.bind(runtime),
        loadPatchBySha: runtime.loadPatchBySha.bind(runtime),
    });
}
function bindComparisonCapability(runtime) {
    requireCapability(runtime, 'comparison', [
        'buildPatchDivergence', 'compareStrand', 'planStrandTransfer',
        'compareCoordinates', 'diff', 'planCoordinateTransfer',
    ]);
    return Object.freeze({
        buildPatchDivergence: runtime.buildPatchDivergence.bind(runtime),
        compareStrand: runtime.compareStrand.bind(runtime),
        planStrandTransfer: runtime.planStrandTransfer.bind(runtime),
        compareCoordinates: runtime.compareCoordinates.bind(runtime),
        diff: runtime.diff.bind(runtime),
        planCoordinateTransfer: runtime.planCoordinateTransfer.bind(runtime),
    });
}
function bindSubscriptionCapability(runtime) {
    requireCapability(runtime, 'subscription', ['subscribe', 'watch']);
    return Object.freeze({
        subscribe: runtime.subscribe.bind(runtime),
        watch: runtime.watch.bind(runtime),
    });
}
/**
 * Opens a WARP multi-writer graph compatibility capability bag.
 *
 * Application workflows should use openWarpWorldline(), which opens a named
 * causal worldline and keeps reads on explicit worldline, coordinate, observer,
 * or optic bases instead of graph-wide materialization.
 *
 * @deprecated For application workflows, use openWarpWorldline(). This advanced
 * compatibility bag remains supported for tooling and substrate diagnostics.
 */
export async function openWarpGraph(deps) {
    const runtime = await openWarpGraphRuntime(deps);
    const query = bindQueryCapability(runtime);
    const patches = bindPatchCapability(runtime);
    const sync = bindSyncCapability(runtime);
    const strands = bindStrandCapability(runtime);
    const checkpoint = bindCheckpointCapability(runtime);
    const provenance = bindProvenanceCapability(runtime);
    const comparison = bindComparisonCapability(runtime);
    const subscriptions = bindSubscriptionCapability(runtime);
    const graph = {
        graphName: runtime.graphName,
        writerId: runtime.writerId,
        // Architectural moments
        commitment: Object.freeze({ patches, strands, comparison }),
        folding: Object.freeze({ checkpoint }),
        revelation: Object.freeze({ query, subscriptions, provenance }),
        governance: Object.freeze({ sync }),
        // Flat aliases
        query, patches, sync, strands,
        checkpoint, provenance, comparison, subscriptions,
    };
    return Object.freeze(graph);
}
