import { useCallback, useEffect, useRef, useState } from 'react';
import { setGlobalQuestionHandler, } from '../../utils/question-queue.js';
import { setGlobalToolApprovalHandler, } from '../../utils/tool-approval-queue.js';
import { setGlobalToolConfirmHandler, } from '../../utils/tool-confirm-queue.js';
/**
 * Wires the two global "ask the user" queues into the React tree:
 *  - question-queue (ask_question tool) drives the question prompt UI
 *  - tool-approval-queue (subagent tool calls) drives a parallel approval flow
 *
 * The tool-approval queue uses a dedicated state slot so it doesn't conflict
 * with the main agent's tool confirmation flow — a subagent's tool can need
 * approval while the parent agent is mid-conversation.
 */
export function useGlobalHandlerQueues({ setPendingQuestion, setIsQuestionMode, }) {
    const questionResolverRef = useRef(null);
    useEffect(() => {
        setGlobalQuestionHandler((question) => {
            return new Promise(resolve => {
                questionResolverRef.current = resolve;
                setPendingQuestion(question);
                setIsQuestionMode(true);
            });
        });
    }, [setPendingQuestion, setIsQuestionMode]);
    const handleQuestionAnswer = useCallback((answer) => {
        if (questionResolverRef.current) {
            questionResolverRef.current(answer);
            questionResolverRef.current = null;
        }
        setIsQuestionMode(false);
        setPendingQuestion(null);
    }, [setIsQuestionMode, setPendingQuestion]);
    const toolApprovalResolverRef = useRef(null);
    const [pendingSubagentApproval, setPendingSubagentApproval] = useState(null);
    useEffect(() => {
        setGlobalToolApprovalHandler((approval) => {
            return new Promise(resolve => {
                toolApprovalResolverRef.current = resolve;
                setPendingSubagentApproval(approval);
                // Don't clear the live component — AgentProgress renders above the
                // chat input, ToolConfirmation renders below. They coexist.
            });
        });
    }, []);
    const handleSubagentToolApproval = useCallback((confirmed) => {
        if (toolApprovalResolverRef.current) {
            toolApprovalResolverRef.current(confirmed);
            toolApprovalResolverRef.current = null;
        }
        setPendingSubagentApproval(null);
    }, []);
    // Main agent tool confirmation: the conversation loop suspends on
    // signalToolConfirm() until the user approves/declines here.
    const toolConfirmResolverRef = useRef(null);
    const [pendingToolConfirmation, setPendingToolConfirmation] = useState(null);
    useEffect(() => {
        setGlobalToolConfirmHandler((confirmation) => {
            return new Promise(resolve => {
                toolConfirmResolverRef.current = resolve;
                setPendingToolConfirmation(confirmation);
            });
        });
    }, []);
    const handleToolConfirmation = useCallback((confirmed) => {
        if (toolConfirmResolverRef.current) {
            toolConfirmResolverRef.current(confirmed);
            toolConfirmResolverRef.current = null;
        }
        setPendingToolConfirmation(null);
    }, []);
    return {
        handleQuestionAnswer,
        pendingSubagentApproval,
        handleSubagentToolApproval,
        pendingToolConfirmation,
        handleToolConfirmation,
    };
}
//# sourceMappingURL=useGlobalHandlerQueues.js.map