"use client";
import { useMemo } from "react";
/**
 * Pure derivation hook that aggregates workspace write-backs across all
 * executions in a session into a flat, deduplicated list.
 *
 * Follows the same pattern as {@link useSessionArtifacts}: `useMemo`-based
 * derivation, no side effects, no data fetching. Takes the same
 * `executions` array input.
 *
 * **Dedup semantics:** Write-backs are keyed by `workspace_entry_name`.
 * When multiple executions write back to the same workspace entry (e.g.,
 * a follow-up execution on the same git repo), the latest execution's
 * write-back wins — each execution creates its own branch/PR, and the
 * most recent one is the one users care about.
 *
 * **Sorting:** Entries are sorted alphabetically by workspace entry name.
 *
 * @param executions - All executions for a session, in chronological
 *   order. Pass both completed and active-stream executions.
 *
 * @example
 * ```tsx
 * const conv = useSessionConversation(sessionId, org);
 * const allExecutions = [
 *   ...conv.completedExecutions,
 *   ...(conv.activeStreamExecution ? [conv.activeStreamExecution] : []),
 * ];
 * const { writeBacks, hasWriteBacks } = useSessionWriteBacks(allExecutions);
 * ```
 *
 * @see useWorkspaceWriteBacks — single-execution write-back derivation
 * @see WriteBackCard — component that renders a single write-back
 */
export function useSessionWriteBacks(executions) {
    return useMemo(() => {
        const entryMap = new Map();
        for (const execution of executions) {
            const executionId = execution.metadata?.id ?? "";
            for (const wb of execution.status?.workspaceWriteBacks ?? []) {
                entryMap.set(wb.workspaceEntryName, {
                    writeBack: wb,
                    executionId,
                });
            }
        }
        const entries = Array.from(entryMap.values());
        entries.sort((a, b) => a.writeBack.workspaceEntryName.localeCompare(b.writeBack.workspaceEntryName, undefined, { sensitivity: "base" }));
        return {
            writeBacks: entries,
            hasWriteBacks: entries.length > 0,
            writeBackCount: entries.length,
        };
    }, [executions]);
}
//# sourceMappingURL=useSessionWriteBacks.js.map