/**
 * WorkflowHooks
 *
 * Orchestrator-facing hooks for workflow step coordination.
 * Exposes setActiveStep, clearActiveStep, notifyWorkflowStep, and waitForTurnDone.
 *
 * Extracted from `InteractiveSessionService` in Phase 6 of the strangler refactor.
 * See `docs/plans/2026-04-14-interactive-session-service-refactor.md`.
 */
export class WorkflowHooks {
    registry;
    dispatcher;
    constructor(registry, dispatcher) {
        this.registry = registry;
        this.dispatcher = dispatcher;
    }
    setActiveStep(featureId, stepId) {
        this.registry.setActiveStep(featureId, stepId);
    }
    clearActiveStep(featureId) {
        this.registry.clearActiveStep(featureId);
    }
    notifyWorkflowStep(featureId, step) {
        this.dispatcher.notifyByFeatureId(featureId, {
            delta: '',
            done: false,
            workflowStep: step,
        });
    }
    /**
     * Resolves the next time any subscriber receives a `done: true`
     * chunk for the given feature.
     */
    async waitForTurnDone(featureId, signal) {
        return new Promise((resolve, reject) => {
            if (signal?.aborted) {
                reject(new Error('waitForTurnDone aborted'));
                return;
            }
            const unsubscribe = this.dispatcher.subscribeByFeature(featureId, (chunk) => {
                if (chunk.done) {
                    unsubscribe();
                    signal?.removeEventListener('abort', onAbort);
                    resolve();
                }
            });
            const onAbort = () => {
                unsubscribe();
                signal?.removeEventListener('abort', onAbort);
                reject(new Error('waitForTurnDone aborted'));
            };
            signal?.addEventListener('abort', onAbort);
        });
    }
}
