import { useCallback, useEffect, useRef } from "react";
export function useAgentInlineDialogState(input) {
    const { activeInlineDialog, setSessionActiveInlineDialog, onQuestionnaireOpenChange, onInlineDialogResolved, } = input;
    const onInlineDialogResolvedRef = useRef(onInlineDialogResolved);
    onInlineDialogResolvedRef.current = onInlineDialogResolved;
    const decorateDialogResolution = useCallback((dialog) => {
        if (!dialog || dialog.__resolutionNotified) {
            return dialog;
        }
        const notifyResolved = (result) => {
            try {
                onInlineDialogResolvedRef.current?.(dialog.request, result);
            }
            catch (error) {
                console.error("[AgentTerminal] Inline dialog resolution hook failed:", error);
            }
        };
        const decorated = {
            ...dialog,
            __resolutionNotified: true,
            onComplete: (result) => {
                notifyResolved(result);
                dialog.onComplete(result);
            },
            onCancel: () => {
                notifyResolved(null);
                dialog.onCancel();
            },
        };
        return decorated;
    }, []);
    const activeInlineDialogRef = useRef(activeInlineDialog);
    const setActiveInlineDialog = useCallback((nextDialog) => {
        const currentDialog = activeInlineDialogRef.current;
        const resolvedDialog = typeof nextDialog === "function"
            ? nextDialog(currentDialog)
            : nextDialog;
        if (Object.is(resolvedDialog, currentDialog)) {
            return;
        }
        const decoratedDialog = decorateDialogResolution(resolvedDialog);
        activeInlineDialogRef.current = decoratedDialog;
        setSessionActiveInlineDialog(decoratedDialog);
    }, [decorateDialogResolution, setSessionActiveInlineDialog]);
    const dialogTerminalInstanceIdRef = useRef("");
    if (!dialogTerminalInstanceIdRef.current) {
        dialogTerminalInstanceIdRef.current = crypto.randomUUID();
    }
    const isQuestionnaireDialogOpen = activeInlineDialog?.request.dialogType === "questionnaire";
    const cancelActiveInlineDialog = useCallback(() => {
        const activeDialog = activeInlineDialogRef.current;
        if (!activeDialog)
            return;
        try {
            activeDialog.onCancel();
        }
        finally {
            setActiveInlineDialog(null);
        }
    }, [setActiveInlineDialog]);
    useEffect(() => {
        onQuestionnaireOpenChange?.(isQuestionnaireDialogOpen);
    }, [isQuestionnaireDialogOpen, onQuestionnaireOpenChange]);
    useEffect(() => {
        return () => {
            onQuestionnaireOpenChange?.(false);
        };
    }, [onQuestionnaireOpenChange]);
    return {
        activeInlineDialog,
        activeInlineDialogRef,
        setActiveInlineDialog,
        cancelActiveInlineDialog,
        dialogTerminalInstanceIdRef,
        isQuestionnaireDialogOpen,
    };
}
//# sourceMappingURL=useAgentInlineDialogState.js.map