"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__, {
    AutopilotChatSources: ()=>AutopilotChatSources
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_namespaceObject = require("@lingui/react");
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_index_cjs_namespaceObject = require("../../../../index.cjs");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const index_cjs_namespaceObject = require("../../../../ap-typography/index.cjs");
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 external_service_index_cjs_namespaceObject = require("../../../service/index.cjs");
const tooltip_cjs_namespaceObject = require("../../common/tooltip.cjs");
const getSources = (group)=>{
    const allCitations = group.flatMap((message)=>message.contentParts?.flatMap((part)=>part.citations) ?? []).filter((citation)=>void 0 !== citation);
    const seenIds = new Set();
    return allCitations.filter((citation)=>{
        if (seenIds.has(citation.id)) return false;
        seenIds.add(citation.id);
        return true;
    });
};
const COLLAPSED_ITEMS_COUNT = 2;
const StyledContainer = (0, material_namespaceObject.styled)(material_namespaceObject.Box)(()=>({
        marginTop: apollo_core_default().Spacing.SpacingXs,
        animation: 'fadeInUp 0.3s ease-out',
        '@keyframes fadeInUp': {
            '0%': {
                opacity: 0,
                transform: `translateY(${apollo_core_default().Spacing.SpacingM})`
            },
            '100%': {
                opacity: 1,
                transform: 'translateY(0)'
            }
        }
    }));
const StyledToggleButton = (0, material_namespaceObject.styled)(material_namespaceObject.Box)(()=>({
        display: 'inline-flex',
        alignItems: 'center',
        gap: apollo_core_default().Spacing.SpacingXs,
        cursor: 'pointer',
        padding: `${apollo_core_default().Spacing.SpacingXs} ${apollo_core_default().Spacing.SpacingBase} ${apollo_core_default().Spacing.SpacingXs} ${apollo_core_default().Spacing.SpacingXs}`,
        borderRadius: apollo_core_default().Border.BorderRadiusL,
        border: "1px solid var(--color-border-de-emp)",
        backgroundColor: 'var(--color-background)',
        '&:hover': {
            backgroundColor: 'var(--color-background-secondary)'
        },
        '&:active, &:focus': {
            outlineColor: 'var(--color-focus-indicator)'
        }
    }));
const StyledContentContainer = (0, material_namespaceObject.styled)(material_namespaceObject.Box)(()=>({
        borderRadius: apollo_core_default().Border.BorderRadiusL,
        marginTop: apollo_core_default().Spacing.SpacingXs,
        border: "1px solid var(--color-border-de-emp)",
        backgroundColor: 'var(--color-background)',
        position: 'relative',
        padding: `${apollo_core_default().Spacing.SpacingXs} 0`
    }));
const StyledSourcesListWrapper = (0, material_namespaceObject.styled)('div')(({ isCollapsed })=>({
        position: 'relative',
        ...isCollapsed && {
            maxHeight: `calc((${apollo_core_default().Spacing.SpacingM} + ${apollo_core_default().Spacing.SpacingS}) * (${COLLAPSED_ITEMS_COUNT} + 0.6))`,
            overflow: 'hidden'
        }
    }));
const StyledSourcesList = (0, material_namespaceObject.styled)(material_namespaceObject.Box)(()=>({
        display: 'flex',
        flexDirection: 'column',
        gap: apollo_core_default().Spacing.SpacingS
    }));
const StyledSourceItem = (0, material_namespaceObject.styled)(material_namespaceObject.Box)(()=>({
        display: 'flex',
        alignItems: 'flex-start',
        gap: apollo_core_default().Spacing.SpacingXs,
        padding: `0 ${apollo_core_default().Spacing.SpacingBase}`,
        cursor: 'pointer',
        transition: 'background-color 0.2s',
        height: apollo_core_default().Spacing.SpacingM,
        '&:hover': {
            backgroundColor: 'var(--color-background-hover)'
        },
        '&:focus, &:active': {
            outlineColor: 'var(--color-focus-indicator)'
        }
    }));
const StyledSourceContent = (0, material_namespaceObject.styled)(material_namespaceObject.Box)(()=>({
        flex: 1,
        whiteSpace: 'nowrap',
        overflow: 'hidden',
        '& .MuiTypography-root': {
            whiteSpace: 'nowrap',
            overflow: 'hidden',
            textOverflow: 'ellipsis'
        }
    }));
const StyledGradientOverlay = (0, material_namespaceObject.styled)(material_namespaceObject.Box)(()=>({
        position: 'absolute',
        bottom: 0,
        left: 0,
        right: 0,
        height: apollo_core_default().Spacing.SpacingM,
        background: "linear-gradient(to bottom, transparent 0%, var(--color-background) 70%)",
        pointerEvents: 'none'
    }));
const StyledActionContainer = (0, material_namespaceObject.styled)(material_namespaceObject.Box)(()=>({
        display: 'flex',
        position: 'relative',
        marginTop: apollo_core_default().Spacing.SpacingXs
    }));
const StyledActionButton = (0, material_namespaceObject.styled)(material_namespaceObject.Box)(()=>({
        display: 'inline-flex',
        alignItems: 'center',
        gap: apollo_core_default().Spacing.SpacingXs,
        cursor: 'pointer',
        padding: `${apollo_core_default().Spacing.SpacingXs} ${apollo_core_default().Spacing.SpacingBase}`,
        borderRadius: apollo_core_default().Border.BorderRadiusM,
        backgroundColor: 'transparent',
        transition: 'all 0.2s',
        border: 'none',
        '&:hover': {
            backgroundColor: 'var(--color-background-hover)'
        },
        '&:focus, &:active': {
            outlineColor: 'var(--color-focus-indicator)'
        }
    }));
function AutopilotChatSourcesComponent({ groupId, message }) {
    const { _ } = (0, react_namespaceObject.useLingui)();
    const chatService = (0, chat_service_provider_cjs_namespaceObject.useChatService)();
    const { spacing } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const [sources, setSources] = (0, external_react_namespaceObject.useState)(getSources(chatService?.getMessagesInGroup(groupId) ?? []));
    const { isStreaming } = (0, use_is_streaming_message_cjs_namespaceObject.useIsStreamingMessage)(message);
    const [isWaitingForMoreMessages, setIsWaitingForMoreMessages] = (0, external_react_namespaceObject.useState)(false);
    const [isExpanded, setIsExpanded] = (0, external_react_namespaceObject.useState)(false);
    const [showFullList, setShowFullList] = (0, external_react_namespaceObject.useState)(false);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!chatService) return;
        const unsubscribeResponse = chatService.on(external_service_index_cjs_namespaceObject.AutopilotChatEvent.Response, (response)=>{
            if (response.groupId === groupId || response.id === message.id) {
                setSources(getSources(chatService.getMessagesInGroup(groupId)));
                setIsWaitingForMoreMessages(!!response.shouldWaitForMoreMessages);
            }
        });
        const unsubscribeChunk = chatService.on(external_service_index_cjs_namespaceObject.AutopilotChatEvent.SendChunk, (chunk)=>{
            if (chunk.groupId === groupId || chunk.id === message.id) setSources(getSources(chatService.getMessagesInGroup(groupId)));
        });
        const unsubscribeStopResponse = chatService.on(external_service_index_cjs_namespaceObject.AutopilotChatEvent.StopResponse, ()=>{
            setSources(getSources(chatService.getMessagesInGroup(groupId)));
        });
        return ()=>{
            unsubscribeResponse();
            unsubscribeChunk();
            unsubscribeStopResponse();
        };
    }, [
        chatService,
        message.id,
        groupId
    ]);
    const handleToggleExpand = (0, external_react_namespaceObject.useCallback)(()=>{
        setIsExpanded(!isExpanded);
    }, [
        isExpanded
    ]);
    const handleShowMore = (0, external_react_namespaceObject.useCallback)(()=>{
        setShowFullList(true);
    }, []);
    const handleShowLess = (0, external_react_namespaceObject.useCallback)(()=>{
        setShowFullList(false);
    }, []);
    const handleSourceClick = (0, external_react_namespaceObject.useCallback)((source)=>{
        if (!chatService) return;
        chatService?.getPreHook(external_service_index_cjs_namespaceObject.AutopilotChatPreHookAction.CitationClick)({
            citation: source
        }).then((proceed)=>{
            if (!proceed) return;
            const url = 'url' in source ? source.url : `${source.download_url}${source.page_number ? `#page=${source.page_number}` : ''}`;
            if (url) window.open(url, '_blank', 'noopener,noreferrer');
        });
    }, [
        chatService
    ]);
    if (isWaitingForMoreMessages || isStreaming || 0 === sources.length) return null;
    const hasMoreItems = sources.length > COLLAPSED_ITEMS_COUNT;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(StyledContainer, {
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(StyledToggleButton, {
                component: "button",
                title: _({
                    id: 'autopilot-chat.message.sources'
                }),
                onClick: handleToggleExpand,
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApIcon, {
                        variant: "outlined",
                        name: "link",
                        size: spacing.compactMode ? apollo_core_default().Icon.IconS : apollo_core_default().Icon.IconM,
                        color: 'var(--color-foreground)'
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                        variant: spacing.primaryFontToken,
                        color: 'var(--color-foreground)',
                        children: _({
                            id: 'autopilot-chat.message.sources'
                        })
                    })
                ]
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(material_namespaceObject.Collapse, {
                in: isExpanded,
                timeout: 300,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(StyledContentContainer, {
                    children: [
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(StyledSourcesListWrapper, {
                            isCollapsed: !showFullList && hasMoreItems,
                            children: [
                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(StyledSourcesList, {
                                    children: sources.map((source, index)=>{
                                        const isUrl = 'url' in source;
                                        const isPdf = 'download_url' in source;
                                        const pageNumber = isPdf && source.page_number ? source.page_number : 0;
                                        const pageText = isPdf && source.page_number ? ` (${_({
                                            id: 'autopilot-chat.message.page-number',
                                            values: {
                                                pageNumber: pageNumber
                                            }
                                        })})` : '';
                                        const text = `[${source.id}] ${source.title}${pageText}`;
                                        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(tooltip_cjs_namespaceObject.AutopilotChatTooltip, {
                                            title: text,
                                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(StyledSourceItem, {
                                                role: "button",
                                                onClick: ()=>handleSourceClick(source),
                                                tabIndex: !showFullList && index > COLLAPSED_ITEMS_COUNT - 1 ? -1 : 0,
                                                children: [
                                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApIcon, {
                                                        variant: isUrl ? 'custom' : 'outlined',
                                                        name: isUrl ? 'website' : 'file_open',
                                                        size: apollo_core_default().Icon.IconXs,
                                                        color: 'var(--color-primary)'
                                                    }),
                                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(StyledSourceContent, {
                                                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                                            variant: spacing.primaryBoldFontToken,
                                                            color: 'var(--color-foreground-de-emp)',
                                                            children: text
                                                        })
                                                    })
                                                ]
                                            })
                                        }, `${source.id}-${index}`);
                                    })
                                }),
                                !showFullList && hasMoreItems && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(StyledGradientOverlay, {})
                            ]
                        }),
                        hasMoreItems && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(StyledActionContainer, {
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(StyledActionButton, {
                                component: "button",
                                title: showFullList ? _({
                                    id: 'autopilot-chat.message.show-less'
                                }) : _({
                                    id: 'autopilot-chat.message.show-more'
                                }),
                                onClick: showFullList ? handleShowLess : handleShowMore,
                                children: [
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApTypography, {
                                        variant: spacing.primaryBoldFontToken,
                                        color: 'var(--color-primary)',
                                        children: showFullList ? _({
                                            id: 'autopilot-chat.message.show-less'
                                        }) : _({
                                            id: 'autopilot-chat.message.show-more'
                                        })
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_cjs_namespaceObject.ApIcon, {
                                        name: showFullList ? 'expand_less' : 'expand_more',
                                        variant: "outlined",
                                        size: apollo_core_default().Icon.IconXs,
                                        color: 'var(--color-primary)'
                                    })
                                ]
                            })
                        })
                    ]
                })
            })
        ]
    });
}
const AutopilotChatSources = /*#__PURE__*/ external_react_default().memo(AutopilotChatSourcesComponent);
exports.AutopilotChatSources = __webpack_exports__.AutopilotChatSources;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AutopilotChatSources"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
