/**
 * dev-workflow-engine.ts — DevWorkflowEngine implementation.
 *
 * Implements WorkflowEngine by delegating to existing NERF state derivation
 * and dispatch logic. This is the "dev" engine — it wraps the current NERF
 * auto-mode behavior behind the engine-polymorphic interface.
 */
import { deriveState } from "./state.js";
import { resolveDispatch } from "./auto-dispatch.js";
import { loadEffectiveNerfPreferences } from "./preferences.js";
// ─── Bridge: DispatchAction → EngineDispatchAction ────────────────────────
/**
 * Map a NERF-specific DispatchAction (which carries `matchedRule`, `unitType`,
 * etc.) to the engine-generic EngineDispatchAction discriminated union.
 *
 * Exported for unit testing.
 */
export function bridgeDispatchAction(da) {
    switch (da.action) {
        case "dispatch":
            return {
                action: "dispatch",
                step: {
                    unitType: da.unitType,
                    unitId: da.unitId,
                    prompt: da.prompt,
                },
            };
        case "stop":
            return {
                action: "stop",
                reason: da.reason,
                level: da.level,
            };
        case "skip":
            return { action: "skip" };
    }
}
// ─── DevWorkflowEngine ───────────────────────────────────────────────────
export class DevWorkflowEngine {
    engineId = "dev";
    async deriveState(basePath) {
        const nerf = await deriveState(basePath);
        return {
            phase: nerf.phase,
            currentMilestoneId: nerf.activeMilestone?.id ?? null,
            activeSliceId: nerf.activeSlice?.id ?? null,
            activeTaskId: nerf.activeTask?.id ?? null,
            isComplete: nerf.phase === "complete",
            raw: nerf,
        };
    }
    async resolveDispatch(state, context) {
        const nerf = state.raw;
        const mid = nerf.activeMilestone?.id ?? "";
        const midTitle = nerf.activeMilestone?.title ?? "";
        const loaded = loadEffectiveNerfPreferences();
        const prefs = loaded?.preferences ?? undefined;
        const dispatchCtx = {
            basePath: context.basePath,
            mid,
            midTitle,
            state: nerf,
            prefs,
        };
        const result = await resolveDispatch(dispatchCtx);
        return bridgeDispatchAction(result);
    }
    async reconcile(state, _completedStep) {
        return {
            outcome: state.isComplete ? "milestone-complete" : "continue",
        };
    }
    getDisplayMetadata(state) {
        return {
            engineLabel: "NERF Dev",
            currentPhase: state.phase,
            progressSummary: `${state.currentMilestoneId ?? "no milestone"} / ${state.activeSliceId ?? "—"} / ${state.activeTaskId ?? "—"}`,
            stepCount: null,
        };
    }
}
