"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__, {
    AutopilotChatMessages: ()=>AutopilotChatMessages
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const material_namespaceObject = require("@mui/material");
const apollo_core_namespaceObject = require("@uipath/apollo-core");
var apollo_core_default = /*#__PURE__*/ __webpack_require__.n(apollo_core_namespaceObject);
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
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 loading_provider_cjs_namespaceObject = require("../../providers/loading-provider.cjs");
const index_cjs_namespaceObject = require("../../service/index.cjs");
const skeleton_loader_cjs_namespaceObject = require("../common/skeleton-loader.cjs");
const external_chat_message_content_cjs_namespaceObject = require("./chat-message-content.cjs");
const chat_fre_cjs_namespaceObject = require("./first-run-experience/chat-fre.cjs");
const chat_loading_cjs_namespaceObject = require("./loader/chat-loading.cjs");
const chat_loading_messages_cjs_namespaceObject = require("./loader/chat-loading-messages.cjs");
const resource_token_parser_cjs_namespaceObject = require("./markdown/parsers/resource-token-parser.cjs");
const chat_suggestions_cjs_namespaceObject = require("./suggestions/chat-suggestions.cjs");
const MessageContainer = (0, material_namespaceObject.styled)('div')(({ isOverflow, isContainerWide, gap })=>({
        display: 'flex',
        flexDirection: 'column',
        gap: `${gap}px`,
        height: '100%',
        ...isOverflow && isContainerWide && {
            position: 'relative',
            left: apollo_core_default().Spacing.SpacingXs
        }
    }));
const removeFakeStream = (messages)=>messages.map(({ fakeStream: _fakeStream, ...rest })=>rest);
const MessageGroupContainer = (0, material_namespaceObject.styled)('div')(({ isAssistant, disableActions, isLastGroup })=>{
    if (disableActions) return isLastGroup ? {
        marginBottom: '14px'
    } : {
        marginBottom: '-16px'
    };
    if (isAssistant) return {
        marginBottom: '30px'
    };
    return {
        marginBottom: '14px'
    };
});
const MessageGroup = /*#__PURE__*/ external_react_default().memo(({ messages, isLastGroup })=>{
    const [groupRef, setGroupRef] = external_react_default().useState(null);
    const disableActions = 1 === messages.length && !!messages[0]?.disableActions;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(MessageGroupContainer, {
        isAssistant: messages[0]?.role === index_cjs_namespaceObject.AutopilotChatRole.Assistant,
        disableActions: disableActions,
        isLastGroup: isLastGroup,
        ref: setGroupRef,
        children: messages.map((message, index)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_chat_message_content_cjs_namespaceObject.AutopilotChatMessageContent, {
                message: message,
                disableActions: disableActions,
                isLastInGroup: index === messages.length - 1,
                containerRef: groupRef
            }, message.id))
    });
});
function AutopilotChatMessagesComponent({ isOverflow, isContainerWide }) {
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const [messages, setMessages] = external_react_default().useState(removeFakeStream(chatService?.getConversation?.() ?? []));
    const { isLoadingMoreMessages, skeletonLoader } = (0, loading_provider_cjs_namespaceObject.useLoading)();
    const { firstRunExperience, setHasMessages, spacing } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const [suggestions, setSuggestions] = external_react_default().useState([]);
    const [sendOnClick, setSendOnClick] = external_react_default().useState(false);
    const updateMessages = external_react_default().useCallback((message)=>{
        setMessages((prev)=>{
            if (prev.some((m)=>m.id === message.id)) return prev.map((m)=>m.id === message.id ? message : m);
            return [
                ...prev,
                message
            ];
        });
    }, []);
    const sendFeedback = external_react_default().useCallback(({ message, action })=>{
        if (!chatService) return;
        if (message.role === index_cjs_namespaceObject.AutopilotChatRole.Assistant) chatService.sendResponse({
            ...message,
            ...message.fakeStream ? {
                fakeStream: false
            } : {},
            ...message.stream ? {
                stream: false,
                done: true
            } : {},
            feedback: {
                isPositive: !!action.details?.isPositive
            }
        });
    }, [
        chatService
    ]);
    const onCopy = external_react_default().useCallback(({ group, message })=>{
        let finalString = '';
        finalString = 0 === group.length ? message.toCopy ?? message.content : group.map((msg)=>msg.toCopy ?? msg.content).join('\n');
        finalString = (0, resource_token_parser_cjs_namespaceObject.stripResourceTokens)(finalString);
        if (navigator.clipboard && finalString) navigator.clipboard.writeText(finalString).catch(()=>{});
    }, []);
    external_react_default().useEffect(()=>{
        if (!chatService) return;
        const unsubscribeRequest = chatService.intercept(index_cjs_namespaceObject.AutopilotChatInterceptableEvent.Request, updateMessages);
        const unsubscribeResponse = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.Response, updateMessages);
        const unsubscribeNewChat = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.NewChat, ()=>setMessages([]));
        const unsubscribeFeedback = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.Feedback, sendFeedback);
        const unsubscribeCopy = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.Copy, onCopy);
        const unsubscribeConversation = chatService.on(index_cjs_namespaceObject.AutopilotChatEvent.SetConversation, (msg)=>{
            setMessages(removeFakeStream(msg));
        });
        const unsubscribeSetSuggestions = chatService.__internalService__.on(index_cjs_namespaceObject.AutopilotChatInternalEvent.SetSuggestions, ({ suggestions: suggestionsToSet, sendOnClick: sendOnClickToSet })=>{
            setSuggestions(suggestionsToSet);
            setSendOnClick(sendOnClickToSet);
        });
        return ()=>{
            unsubscribeRequest();
            unsubscribeResponse();
            unsubscribeConversation();
            unsubscribeNewChat();
            unsubscribeFeedback();
            unsubscribeCopy();
            unsubscribeSetSuggestions();
        };
    }, [
        chatService,
        updateMessages,
        sendFeedback,
        onCopy
    ]);
    const messageGroups = (0, external_react_namespaceObject.useMemo)(()=>messages.reduce((acc, message, index)=>{
            if (message.groupId) if (index > 0 && messages[index - 1]?.groupId === message.groupId) acc[acc.length - 1].push(message);
            else acc.push([
                message
            ]);
            else acc.push([
                message
            ]);
            return acc;
        }, []), [
        messages
    ]);
    external_react_default().useEffect(()=>{
        setHasMessages(messages.length > 0);
    }, [
        messages,
        setHasMessages
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(MessageContainer, {
        gap: spacing.messageSpacing,
        isOverflow: isOverflow,
        isContainerWide: isContainerWide,
        children: skeletonLoader ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(skeleton_loader_cjs_namespaceObject.SkeletonLoader, {}) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
            children: [
                isLoadingMoreMessages && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(chat_loading_messages_cjs_namespaceObject.AutopilotChatLoadingMessages, {}),
                0 === messages.length && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(chat_fre_cjs_namespaceObject.AutopilotChatFRE, {}),
                messageGroups.map((group, idx)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(MessageGroup, {
                        messages: group,
                        isLastGroup: idx === messageGroups.length - 1
                    }, group[0]?.id)),
                messages.length > 0 && suggestions.length > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(chat_suggestions_cjs_namespaceObject.AutopilotChatSuggestions, {
                    suggestions: suggestions,
                    sendOnClick: sendOnClick ?? firstRunExperience?.sendOnClick ?? false,
                    includeTitle: true
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(chat_loading_cjs_namespaceObject.AutopilotChatLoading, {})
            ]
        })
    });
}
const AutopilotChatMessages = /*#__PURE__*/ external_react_default().memo(AutopilotChatMessagesComponent);
exports.AutopilotChatMessages = __webpack_exports__.AutopilotChatMessages;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotChatMessages"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
