import { useCallback, useEffect, useRef } from "react";
export function useAgentInlineDialogState(input) {
    const { activeInlineDialog, setSessionActiveInlineDialog, onQuestionnaireOpenChange, } = input;
    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;
        }
        activeInlineDialogRef.current = resolvedDialog;
        setSessionActiveInlineDialog(resolvedDialog);
    }, [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