"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    AutopilotChatMessageActions: ()=>AutopilotChatMessageActions
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_namespaceObject = require("@lingui/react");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const use_is_streaming_message_cjs_namespaceObject = require("../../../hooks/use-is-streaming-message.cjs");
const chat_service_provider_cjs_namespaceObject = require("../../../providers/chat-service.provider.cjs");
const chat_state_provider_cjs_namespaceObject = require("../../../providers/chat-state-provider.cjs");
const index_cjs_namespaceObject = require("../../../service/index.cjs");
const external_chat_actions_list_cjs_namespaceObject = require("./chat-actions-list.cjs");
function AutopilotChatMessageActionsComponent({ message, containerElement }) {
    const { _ } = (0, react_namespaceObject.useLingui)();
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const { disabledFeatures, readOnly } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const [isLastAssistantMessage, setIsLastAssistantMessage] = external_react_default().useState(false);
    const [isVisible, setIsVisible] = external_react_default().useState(false);
    const isUserInteractingWithActions = external_react_default().useRef(false);
    const actionsContainerRef = external_react_default().useRef(null);
    const { isStreaming } = (0, use_is_streaming_message_cjs_namespaceObject.useIsStreamingMessage)(message);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!chatService) return;
        const assistantMessages = chatService.getConversation?.()?.filter((m)=>m.role === index_cjs_namespaceObject.AutopilotChatRole.Assistant);
        const lastAssistantMessage = assistantMessages?.[assistantMessages.length - 1];
        if (lastAssistantMessage?.id === message.id || lastAssistantMessage?.groupId === message.groupId) setIsLastAssistantMessage(true);
        const unsubscribeResponse = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.Response, (response)=>{
            setIsLastAssistantMessage(response.id === lastAssistantMessage?.id || response.groupId === lastAssistantMessage?.groupId);
        });
        return ()=>{
            unsubscribeResponse();
        };
    }, [
        chatService,
        message
    ]);
    const defaultActions = external_react_default().useMemo(()=>{
        const baseActions = [];
        if (!disabledFeatures.copy) baseActions.push({
            name: 'autopilot-chat-copy',
            label: _({
                id: 'autopilot-chat.message.actions.copy'
            }),
            icon: 'content_copy',
            eventName: index_cjs_namespaceObject.AutopilotChatEvent.Copy
        });
        if (message.role !== index_cjs_namespaceObject.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: index_cjs_namespaceObject.AutopilotChatEvent.Feedback,
                details: {
                    isPositive: true,
                    preHookAction: index_cjs_namespaceObject.AutopilotChatPreHookAction.Feedback
                }
            },
            {
                name: 'autopilot-chat-bad',
                label: _({
                    id: 'autopilot-chat.message.actions.bad'
                }),
                icon: 'thumb_down',
                eventName: index_cjs_namespaceObject.AutopilotChatEvent.Feedback,
                details: {
                    isPositive: false,
                    preHookAction: index_cjs_namespaceObject.AutopilotChatPreHookAction.Feedback
                }
            }
        ];
    }, [
        message,
        readOnly,
        disabledFeatures.feedback,
        disabledFeatures.copy,
        _
    ]);
    const isRelatedTarget = external_react_default().useCallback((target)=>actionsContainerRef.current && (actionsContainerRef.current === target || actionsContainerRef.current.contains(target)), []);
    const handleActionContainerRef = external_react_default().useCallback((node)=>{
        actionsContainerRef.current = node;
    }, []);
    const handleActionsHoverChange = external_react_default().useCallback((isHovering)=>{
        isUserInteractingWithActions.current = isHovering;
        if (isHovering) setIsVisible(true);
    }, []);
    external_react_default().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__*/ (0, jsx_runtime_namespaceObject.jsx)(external_chat_actions_list_cjs_namespaceObject.AutopilotChatActionsList, {
        message: message,
        defaultActions: defaultActions,
        isVisible: isVisible || isLastAssistantMessage,
        setIsVisible: setIsVisible,
        onHoverChange: handleActionsHoverChange,
        actionsContainerRef: handleActionContainerRef
    });
}
const AutopilotChatMessageActions = /*#__PURE__*/ external_react_default().memo(AutopilotChatMessageActionsComponent);
exports.AutopilotChatMessageActions = __webpack_exports__.AutopilotChatMessageActions;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotChatMessageActions"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
