"use client";
import { useCallback, useContext, useSyncExternalStore } from "react";
import { ThreadSelectionContext } from "./ThreadSelectionContext";
const NOOP_SUBSCRIBE = (_cb) => () => { };
const NOOP_SNAPSHOT = () => false;
/**
 * Per-item selection hook for thread leaves.
 *
 * Uses `useSyncExternalStore` so only the previously- and
 * newly-selected rows re-render on a selection change (≤2 rows).
 *
 * Returns `null` when `ThreadSelectionContext` is absent (DD-011:
 * opt-in, backward compatible). Callers should guard:
 *
 * ```tsx
 * const selection = useThreadSelection("tool-call", tc.id);
 * // selection is null when no provider → no selection affordance
 * ```
 */
export function useThreadSelection(kind, id) {
    const store = useContext(ThreadSelectionContext);
    const subscribe = store?.subscribe ?? NOOP_SUBSCRIBE;
    const getSnapshot = store
        ? () => store.isSelected(kind, id)
        : NOOP_SNAPSHOT;
    const isSelected = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
    const select = useCallback(() => {
        if (!store)
            return;
        const item = buildItem(kind, id);
        store.toggle(item);
    }, [store, kind, id]);
    if (!store)
        return null;
    return { isSelected, select };
}
/**
 * Hook to read the full selection from the store (for the InspectTab).
 *
 * Returns `null` when no provider is present or nothing is selected.
 */
export function useSelectedThreadItem() {
    const store = useContext(ThreadSelectionContext);
    const subscribe = store?.subscribe ?? NOOP_SUBSCRIBE;
    const getSnapshot = store
        ? store.getSelection
        : () => null;
    return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function buildItem(kind, id) {
    switch (kind) {
        case "tool-call":
            return { kind: "tool-call", toolCallId: id };
        case "sub-agent":
            return { kind: "sub-agent", subAgentId: id };
        case "artifact":
            return { kind: "artifact", artifactKey: id };
        case "write-back":
            return { kind: "write-back", workspaceEntryName: id };
    }
}
//# sourceMappingURL=useThreadSelection.js.map