"use client";
import { useMemo } from "react";
/**
 * Pure derivation hook that selects the currently active task from the
 * task states map. Returns referentially stable output (string comparison,
 * not array/object reference) to avoid downstream re-renders.
 *
 * "Active" means either `running` or `waiting_approval` — both represent
 * states where the execution is at a specific task and the user cares
 * about its identity.
 *
 * When multiple tasks are running simultaneously (fork branches), returns
 * the first one found with a `concurrentCount` indicating parallelism.
 * The indicator UI can use this to show "N tasks running in parallel".
 */
export function useActiveTaskName(taskStates) {
    return useMemo(() => {
        let firstRunning = null;
        let runningCount = 0;
        for (const [name, state] of taskStates) {
            if (state.status === "waiting_approval") {
                // Waiting approval takes priority — it's an actionable state
                return {
                    taskName: name,
                    status: "waiting_approval",
                    durationMs: state.durationMs,
                    agentSlug: state.agentSlug,
                    currentToolName: state.currentToolName,
                    concurrentCount: 1,
                };
            }
            if (state.status === "running") {
                runningCount++;
                if (!firstRunning) {
                    firstRunning = {
                        taskName: name,
                        status: "running",
                        durationMs: state.durationMs,
                        agentSlug: state.agentSlug,
                        currentToolName: state.currentToolName,
                        concurrentCount: 1,
                    };
                }
            }
        }
        if (firstRunning && runningCount > 1) {
            return { ...firstRunning, concurrentCount: runningCount };
        }
        return firstRunning;
    }, [taskStates]);
}
//# sourceMappingURL=useActiveTaskName.js.map