"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__, {
    AutopilotChatMessageContent: ()=>AutopilotChatMessageContent
});
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 index_cjs_namespaceObject = require("../../../ap-tool-call/index.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 locale_provider_cjs_namespaceObject = require("../../providers/locale-provider.cjs");
const external_service_index_cjs_namespaceObject = require("../../service/index.cjs");
const dynamic_padding_cjs_namespaceObject = require("../../utils/dynamic-padding.cjs");
const attachments_cjs_namespaceObject = require("../common/attachments.cjs");
const chat_actions_cjs_namespaceObject = require("./actions/chat-actions.cjs");
const markdown_cjs_namespaceObject = require("./markdown/markdown.cjs");
const chat_sources_cjs_namespaceObject = require("./sources/chat-sources.cjs");
const tree_renderer_cjs_namespaceObject = require("./tree/tree-renderer.cjs");
const getApolloMessageRenderers = (locale)=>[
        {
            name: external_service_index_cjs_namespaceObject.DEFAULT_MESSAGE_RENDERER,
            component: markdown_cjs_namespaceObject.AutopilotChatMarkdownRenderer
        },
        {
            name: external_service_index_cjs_namespaceObject.AGENTS_TOOL_CALL_RENDERER,
            component: ({ message })=>{
                if (!message.meta.span && !message.meta.input && !message.meta.toolName) return null;
                return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApToolCall, {
                    span: message.meta.span,
                    toolName: message.meta.toolName,
                    input: message.meta.input,
                    output: message.meta.output,
                    isError: message.meta.isError,
                    startTime: message.meta.startTime,
                    endTime: message.meta.endTime,
                    displayMode: message.meta.displayMode,
                    locale: locale
                });
            }
        },
        {
            name: external_service_index_cjs_namespaceObject.APOLLO_CHAT_TREE_RENDERER,
            component: ({ message })=>{
                if (!message.meta?.span) return null;
                return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(tree_renderer_cjs_namespaceObject.ApolloChatTreeRenderer, {
                    span: message.meta.span
                });
            }
        }
    ];
const MessageBoxComponent = (0, material_namespaceObject.styled)('div')(({ isAssistant, isCustomWidget })=>{
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const { spacing } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const chatInternalService = chatService.__internalService__;
    const [padding, setPadding] = external_react_default().useState((0, dynamic_padding_cjs_namespaceObject.calculateDynamicPadding)(parseInt(external_service_index_cjs_namespaceObject.StorageService.Instance.get(external_service_index_cjs_namespaceObject.CHAT_WIDTH_KEY) ?? external_service_index_cjs_namespaceObject.CHAT_WIDTH_SIDE_BY_SIDE_MIN.toString(), 10)));
    external_react_default().useEffect(()=>{
        if (!chatInternalService || !chatService) return;
        const unsubscribeResize = chatInternalService.on(external_service_index_cjs_namespaceObject.AutopilotChatInternalEvent.ChatResize, (width)=>{
            setPadding((0, dynamic_padding_cjs_namespaceObject.calculateDynamicPadding)(width));
        });
        const unsubscribeMode = chatService.on(external_service_index_cjs_namespaceObject.AutopilotChatEvent.ModeChange, (mode)=>{
            if (mode === external_service_index_cjs_namespaceObject.AutopilotChatMode.FullScreen) setPadding(external_service_index_cjs_namespaceObject.CHAT_MESSAGE_MAX_PADDING);
            else {
                const defaultWidth = external_service_index_cjs_namespaceObject.CHAT_WIDTH_SIDE_BY_SIDE_MIN.toString();
                const storedWidth = external_service_index_cjs_namespaceObject.StorageService.Instance.get(external_service_index_cjs_namespaceObject.CHAT_WIDTH_KEY) ?? defaultWidth;
                const width = parseInt(storedWidth, 10);
                setPadding((0, dynamic_padding_cjs_namespaceObject.calculateDynamicPadding)(width));
            }
        });
        return ()=>{
            unsubscribeResize();
            unsubscribeMode();
        };
    }, [
        chatInternalService,
        chatService
    ]);
    return {
        display: 'flex',
        padding: isAssistant ? 0 : apollo_core_default().Spacing.SpacingBase,
        paddingRight: isAssistant ? apollo_core_default().Spacing.SpacingXs : `calc(${apollo_core_default().Spacing.SpacingBase} + ${apollo_core_default().Spacing.SpacingXl})`,
        flexDirection: 'column',
        justifyContent: 'center',
        alignSelf: isAssistant ? 'flex-start' : 'flex-end',
        gap: spacing.messageGroupGap,
        borderRadius: apollo_core_default().Border.BorderRadiusL,
        backgroundColor: isAssistant ? 'unset' : "var(--custom-autopilot-chat-user-message-bg-color, var(--color-background-secondary))",
        marginLeft: isAssistant ? '0' : `${padding}px`,
        marginRight: isAssistant ? apollo_core_default().Spacing.SpacingXl : '0',
        whiteSpace: 'normal',
        overflowWrap: 'anywhere',
        position: 'relative',
        ...isCustomWidget && {
            width: '100%'
        }
    };
});
const MessageBox = /*#__PURE__*/ external_react_default().memo(MessageBoxComponent);
const WidgetContainer = /*#__PURE__*/ external_react_default().memo(({ message, isLastInGroup, containerRef })=>{
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const unsubscribeRef = external_react_default().useRef(()=>{});
    external_react_default().useEffect(()=>()=>{
            unsubscribeRef?.current?.();
        }, []);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(MessageBox, {
        isAssistant: message.role === external_service_index_cjs_namespaceObject.AutopilotChatRole.Assistant,
        isCustomWidget: true,
        children: [
            message.attachments && message.attachments.length > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(attachments_cjs_namespaceObject.Attachments, {
                attachments: message.attachments,
                removeSpacing: true,
                disableOverflow: true
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                className: "chat-widget-container",
                ref: (el)=>{
                    if (el) {
                        const unsubscribe = chatService.renderMessage(el, message);
                        if (unsubscribe) unsubscribeRef.current = unsubscribe;
                    }
                }
            }),
            isLastInGroup && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                children: [
                    message.role === external_service_index_cjs_namespaceObject.AutopilotChatRole.Assistant && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(chat_sources_cjs_namespaceObject.AutopilotChatSources, {
                        groupId: message.groupId ?? '',
                        message: message
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(chat_actions_cjs_namespaceObject.AutopilotChatMessageActions, {
                        message: message,
                        containerElement: containerRef
                    })
                ]
            })
        ]
    });
});
function AutopilotChatMessageContentComponent({ message, isLastInGroup = true, disableActions = false, containerRef }) {
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const { locale } = (0, locale_provider_cjs_namespaceObject.useLocale)();
    if (!message.content && !message.contentParts && !message.attachments) return null;
    if (!chatService.getMessageRenderer(message.widget)) {
        const APOLLO_MESSAGE_RENDERERS = getApolloMessageRenderers(locale);
        const ApolloMessageRenderer = APOLLO_MESSAGE_RENDERERS.find((renderer)=>renderer.name === message.widget)?.component;
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(MessageBox, {
            isAssistant: message.role === external_service_index_cjs_namespaceObject.AutopilotChatRole.Assistant,
            id: message.id,
            children: [
                message.attachments && message.attachments.length > 0 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(attachments_cjs_namespaceObject.Attachments, {
                    attachments: message.attachments,
                    removeSpacing: true,
                    disableOverflow: true
                }),
                ApolloMessageRenderer ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ApolloMessageRenderer, {
                    message: message
                }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(markdown_cjs_namespaceObject.AutopilotChatMarkdownRenderer, {
                    message: message
                }),
                isLastInGroup && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                    children: [
                        message.role === external_service_index_cjs_namespaceObject.AutopilotChatRole.Assistant && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(chat_sources_cjs_namespaceObject.AutopilotChatSources, {
                            groupId: message.groupId ?? '',
                            message: message
                        }),
                        !disableActions && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(chat_actions_cjs_namespaceObject.AutopilotChatMessageActions, {
                            message: message,
                            containerElement: containerRef
                        })
                    ]
                })
            ]
        }, message.id);
    }
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(WidgetContainer, {
        message: message,
        isLastInGroup: isLastInGroup,
        containerRef: containerRef
    }, message.id);
}
const AutopilotChatMessageContent = /*#__PURE__*/ external_react_default().memo(AutopilotChatMessageContentComponent);
exports.AutopilotChatMessageContent = __webpack_exports__.AutopilotChatMessageContent;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotChatMessageContent"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
