import { jsx } from "react/jsx-runtime";
import { useLingui } from "@lingui/react";
import react, { useEffect } from "react";
import { useIsStreamingMessage } from "../../../hooks/use-is-streaming-message.js";
import { useChatService } from "../../../providers/chat-service.provider.js";
import { useChatState } from "../../../providers/chat-state-provider.js";
import { AutopilotChatEvent, AutopilotChatPreHookAction, AutopilotChatRole } from "../../../service/index.js";
import { AutopilotChatActionsList } from "./chat-actions-list.js";
function AutopilotChatMessageActionsComponent({ message, containerElement }) {
    const { _ } = useLingui();
    const chatService = useChatService();
    const { disabledFeatures, readOnly } = useChatState();
    const [isLastAssistantMessage, setIsLastAssistantMessage] = react.useState(false);
    const [isVisible, setIsVisible] = react.useState(false);
    const isUserInteractingWithActions = react.useRef(false);
    const actionsContainerRef = react.useRef(null);
    const { isStreaming } = useIsStreamingMessage(message);
    useEffect(()=>{
        if (!chatService) return;
        const assistantMessages = chatService.getConversation?.()?.filter((m)=>m.role === AutopilotChatRole.Assistant);
        const lastAssistantMessage = assistantMessages?.[assistantMessages.length - 1];
        if (lastAssistantMessage?.id === message.id || lastAssistantMessage?.groupId === message.groupId) setIsLastAssistantMessage(true);
        const unsubscribeResponse = chatService.on(AutopilotChatEvent.Response, (response)=>{
            setIsLastAssistantMessage(response.id === lastAssistantMessage?.id || response.groupId === lastAssistantMessage?.groupId);
        });
        return ()=>{
            unsubscribeResponse();
        };
    }, [
        chatService,
        message
    ]);
    const defaultActions = react.useMemo(()=>{
        const baseActions = [];
        if (!disabledFeatures.copy) baseActions.push({
            name: 'autopilot-chat-copy',
            label: _({
                id: 'autopilot-chat.message.actions.copy'
            }),
            icon: 'content_copy',
            eventName: AutopilotChatEvent.Copy
        });
        if (message.role !== AutopilotChatRole.Assistant || readOnly || disabledFeatures.feedback) return baseActions;
        if (message.feedback) {
            const { isPositive } = message.feedback;
            return [
                ...baseActions,
                {
                    name: isPositive ? 'autopilot-chat-good' : 'autopilot-chat-bad',
                    label: isPositive ? _({
                        id: 'autopilot-chat.message.actions.good'
                    }) : _({
                        id: 'autopilot-chat.message.actions.bad'
                    }),
                    icon: isPositive ? 'thumb_up' : 'thumb_down',
                    disabled: true
                }
            ];
        }
        return [
            ...baseActions,
            {
                name: 'autopilot-chat-good',
                label: _({
                    id: 'autopilot-chat.message.actions.good'
                }),
                icon: 'thumb_up',
                eventName: AutopilotChatEvent.Feedback,
                details: {
                    isPositive: true,
                    preHookAction: AutopilotChatPreHookAction.Feedback
                }
            },
            {
                name: 'autopilot-chat-bad',
                label: _({
                    id: 'autopilot-chat.message.actions.bad'
                }),
                icon: 'thumb_down',
                eventName: AutopilotChatEvent.Feedback,
                details: {
                    isPositive: false,
                    preHookAction: AutopilotChatPreHookAction.Feedback
                }
            }
        ];
    }, [
        message,
        readOnly,
        disabledFeatures.feedback,
        disabledFeatures.copy,
        _
    ]);
    const isRelatedTarget = react.useCallback((target)=>actionsContainerRef.current && (actionsContainerRef.current === target || actionsContainerRef.current.contains(target)), []);
    const handleActionContainerRef = react.useCallback((node)=>{
        actionsContainerRef.current = node;
    }, []);
    const handleActionsHoverChange = react.useCallback((isHovering)=>{
        isUserInteractingWithActions.current = isHovering;
        if (isHovering) setIsVisible(true);
    }, []);
    react.useEffect(()=>{
        if (!containerElement) return;
        const handleMouseEnter = ()=>setIsVisible(true);
        const handleMouseLeave = (event)=>{
            if (isUserInteractingWithActions.current) return;
            if (isRelatedTarget(event.relatedTarget)) return;
            setIsVisible(false);
        };
        const handleFocus = ()=>setIsVisible(true);
        const handleBlur = (event)=>{
            const relatedTarget = event.relatedTarget;
            if (isRelatedTarget(relatedTarget) || containerElement.contains(relatedTarget)) return;
            setIsVisible(false);
        };
        containerElement.addEventListener('mouseenter', handleMouseEnter);
        containerElement.addEventListener('mouseleave', handleMouseLeave);
        containerElement.addEventListener('focusin', handleFocus);
        containerElement.addEventListener('focusout', handleBlur);
        return ()=>{
            containerElement.removeEventListener('mouseenter', handleMouseEnter);
            containerElement.removeEventListener('mouseleave', handleMouseLeave);
            containerElement.removeEventListener('focusin', handleFocus);
            containerElement.removeEventListener('focusout', handleBlur);
        };
    }, [
        containerElement,
        isRelatedTarget
    ]);
    if (isStreaming) return null;
    return /*#__PURE__*/ jsx(AutopilotChatActionsList, {
        message: message,
        defaultActions: defaultActions,
        isVisible: isVisible || isLastAssistantMessage,
        setIsVisible: setIsVisible,
        onHoverChange: handleActionsHoverChange,
        actionsContainerRef: handleActionContainerRef
    });
}
const AutopilotChatMessageActions = /*#__PURE__*/ react.memo(AutopilotChatMessageActionsComponent);
export { AutopilotChatMessageActions };
