import { emitOrchestrationGraphCreated, emitOrchestrationNodeAdded, emitOrchestrationNodeCompleted, emitOrchestrationNodeFailed, emitOrchestrationNodeStarted, emitWorkflowAutoCommitted, emitWorkflowCascadeAborted, emitWorkflowChainCreated, emitWorkflowChainPassed, emitWorkflowGateResult, emitWorkflowStateChanged, } from '../runtime/emitters/index.js';
export function createWrfcWorkflowContext(sessionId, chainId) {
    return {
        sessionId,
        traceId: `${sessionId}:workflow:${chainId}`,
        source: 'wrfc-controller',
    };
}
export function createWrfcOrchestrationGraphId(chainId) {
    return `wrfc:${chainId}`;
}
export function emitWrfcStateChanged(runtimeBus, sessionId, chainId, from, to) {
    emitWorkflowStateChanged(runtimeBus, createWrfcWorkflowContext(sessionId, chainId), { chainId, from, to });
}
export function emitWrfcChainCreated(runtimeBus, sessionId, chainId, task) {
    emitWorkflowChainCreated(runtimeBus, createWrfcWorkflowContext(sessionId, chainId), { chainId, task });
}
export function emitWrfcGateResult(runtimeBus, sessionId, chainId, gate, passed) {
    emitWorkflowGateResult(runtimeBus, createWrfcWorkflowContext(sessionId, chainId), { chainId, gate, passed });
}
export function emitWrfcChainPassed(runtimeBus, sessionId, chainId) {
    emitWorkflowChainPassed(runtimeBus, createWrfcWorkflowContext(sessionId, chainId), { chainId });
}
export function emitWrfcAutoCommitted(runtimeBus, sessionId, chainId, commitHash) {
    emitWorkflowAutoCommitted(runtimeBus, createWrfcWorkflowContext(sessionId, chainId), { chainId, commitHash });
}
export function emitWrfcCascadeAbort(runtimeBus, sessionId, chainId, reason) {
    emitWorkflowCascadeAborted(runtimeBus, createWrfcWorkflowContext(sessionId, chainId), { chainId, reason });
}
export function emitWrfcGraphCreated(runtimeBus, sessionId, chainId, title) {
    emitOrchestrationGraphCreated(runtimeBus, createWrfcWorkflowContext(sessionId, chainId), {
        graphId: createWrfcOrchestrationGraphId(chainId),
        title,
        mode: 'review-loop',
    });
}
export function startWrfcOrchestrationNode(runtimeBus, sessionId, chainId, suffix, role, title, agentId) {
    const nodeId = `${chainId}:${suffix}`;
    const context = {
        sessionId,
        traceId: `${sessionId}:orchestration:${chainId}:${suffix}`,
        source: 'wrfc-controller',
        ...(agentId !== undefined ? { agentId } : {}),
    };
    emitOrchestrationNodeAdded(runtimeBus, context, {
        graphId: createWrfcOrchestrationGraphId(chainId),
        nodeId,
        title,
        role,
        ...(agentId !== undefined ? { agentId } : {}),
    });
    emitOrchestrationNodeStarted(runtimeBus, context, {
        graphId: createWrfcOrchestrationGraphId(chainId),
        nodeId,
        ...(agentId !== undefined ? { agentId } : {}),
    });
    return nodeId;
}
export function completeWrfcOrchestrationNode(runtimeBus, sessionId, chainId, nodeId, summary) {
    emitOrchestrationNodeCompleted(runtimeBus, {
        sessionId,
        traceId: `${sessionId}:orchestration:${nodeId}:complete`,
        source: 'wrfc-controller',
    }, {
        graphId: createWrfcOrchestrationGraphId(chainId),
        nodeId,
        ...(summary !== undefined ? { summary } : {}),
    });
}
export function failWrfcOrchestrationNode(runtimeBus, sessionId, chainId, nodeId, error) {
    emitOrchestrationNodeFailed(runtimeBus, {
        sessionId,
        traceId: `${sessionId}:orchestration:${nodeId}:fail`,
        source: 'wrfc-controller',
    }, {
        graphId: createWrfcOrchestrationGraphId(chainId),
        nodeId,
        error,
    });
}
