"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__, {
    Code: ()=>Code
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_namespaceObject = require("@lingui/react");
const Close_namespaceObject = require("@mui/icons-material/Close");
var Close_default = /*#__PURE__*/ __webpack_require__.n(Close_namespaceObject);
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_katex_namespaceObject = require("katex");
var external_katex_default = /*#__PURE__*/ __webpack_require__.n(external_katex_namespaceObject);
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const external_react_syntax_highlighter_namespaceObject = require("react-syntax-highlighter");
const prism_namespaceObject = require("react-syntax-highlighter/dist/esm/styles/prism");
const index_cjs_namespaceObject = require("../../../../ap-chip/index.cjs");
const chat_state_provider_cjs_namespaceObject = require("../../../providers/chat-state-provider.cjs");
const theme_provider_cjs_namespaceObject = require("../../../providers/theme-provider.cjs");
const action_button_cjs_namespaceObject = require("../../common/action-button.cjs");
var code_LANGUAGES = /*#__PURE__*/ function(LANGUAGES) {
    LANGUAGES["HTML"] = "html";
    return LANGUAGES;
}(code_LANGUAGES || {});
const extractTextFromChildren = (children)=>{
    if ('string' == typeof children) return children;
    if ('number' == typeof children) return String(children);
    if (/*#__PURE__*/ external_react_default().isValidElement(children)) {
        const element = children;
        return extractTextFromChildren(element.props.children);
    }
    if (Array.isArray(children)) return children.map((child)=>extractTextFromChildren(child)).join('');
    return '';
};
const CodeBlockHeader = (0, material_namespaceObject.styled)('div')(()=>({
        display: 'flex',
        width: '100%',
        alignItems: 'center',
        justifyContent: 'space-between',
        padding: `${apollo_core_default().Spacing.SpacingXs} ${apollo_core_default().Spacing.SpacingBase}`,
        color: 'var(--color-foreground)',
        fontFamily: apollo_core_default().FontFamily.FontNormal,
        boxSizing: 'border-box'
    }));
const CodeBlockContainer = (0, material_namespaceObject.styled)('div')(()=>({
        position: 'relative',
        width: '100%',
        fontFamily: apollo_core_default().FontFamily.FontNormal,
        background: 'var(--color-background)',
        borderRadius: apollo_core_default().Border.BorderRadiusL,
        border: "1px solid var(--color-background-gray)",
        paddingBottom: apollo_core_default().Spacing.SpacingMicro,
        '&:not(pre &)': {
            display: 'inline-block',
            width: 'unset',
            position: 'relative',
            top: '6px',
            borderRadius: 0,
            border: 'none',
            background: 'var(--color-background-disabled)',
            padding: `0 ${apollo_core_default().Spacing.SpacingMicro}`,
            '& *': {
                padding: '0 !important'
            },
            [`& ${CodeBlockHeader}`]: {
                display: 'none !important'
            }
        }
    }));
const NonCodeContentContainer = (0, material_namespaceObject.styled)('div')(()=>({
        color: 'var(--color-foreground)',
        padding: apollo_core_default().Spacing.SpacingM,
        maxWidth: '100%',
        overflow: 'auto',
        fontFamily: apollo_core_default().FontFamily.FontNormal
    }));
const SyntaxHighlighterContainer = (0, material_namespaceObject.styled)('div')(()=>({
        margin: 0,
        width: '100%',
        borderRadius: apollo_core_default().Border.BorderRadiusL,
        maxWidth: '100%',
        '& pre': {
            margin: '0 !important',
            borderRadius: `${apollo_core_default().Border.BorderRadiusL} !important`
        }
    }));
const ContentContainer = (0, material_namespaceObject.styled)('div')(()=>({
        display: 'flex',
        flexDirection: 'column',
        width: '100%',
        height: '400px',
        borderRadius: apollo_core_default().Border.BorderRadiusL,
        overflow: 'auto',
        backgroundColor: 'var(--color-background)',
        boxSizing: 'border-box',
        resize: 'vertical'
    }));
const PreviewIframe = (0, material_namespaceObject.styled)('iframe')(()=>({
        width: '100%',
        height: '100%',
        border: 'none',
        backgroundColor: 'var(--color-background)',
        flex: 1
    }));
const CustomModal = (0, material_namespaceObject.styled)(material_namespaceObject.Modal)(()=>({
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        '& .MuiBackdrop-root': {
            backgroundColor: 'rgba(0, 0, 0, 0.5)'
        }
    }));
const ModalContainer = (0, material_namespaceObject.styled)(material_namespaceObject.Box)(()=>({
        position: 'absolute',
        top: '50%',
        left: '50%',
        transform: 'translate(-50%, -50%)',
        width: '95vw',
        height: '90vh',
        maxWidth: '1400px',
        maxHeight: '900px',
        backgroundColor: 'var(--color-background-raised)',
        borderRadius: apollo_core_default().Border.BorderRadiusS,
        padding: apollo_core_default().Spacing.SpacingL,
        boxShadow: '0px 5px 5px -3px rgba(0, 0, 0, 0.20), 0px 8px 10px 1px rgba(0, 0, 0, 0.14), 0px 3px 14px 2px rgba(0, 0, 0, 0.12)',
        display: 'flex',
        flexDirection: 'column',
        overflow: 'hidden',
        outline: 'none',
        '&:focus-visible, &:focus': {
            outline: 'none'
        }
    }));
const ModalContent = (0, material_namespaceObject.styled)(material_namespaceObject.Box)(()=>({
        flex: 1,
        display: 'flex',
        flexDirection: 'column',
        overflow: 'hidden'
    }));
const CloseButton = (0, material_namespaceObject.styled)(material_namespaceObject.IconButton)(()=>({
        position: 'absolute',
        top: apollo_core_default().Spacing.SpacingXs,
        right: apollo_core_default().Spacing.SpacingS,
        zIndex: 1,
        backgroundColor: 'var(--color-background-raised)',
        '&:hover': {
            backgroundColor: 'var(--color-background-hover)'
        }
    }));
const Code = /*#__PURE__*/ external_react_default().memo(({ inline, className, children, isStreaming, ...props })=>{
    const { _ } = (0, react_namespaceObject.useLingui)();
    const { disabledFeatures, spacing, portalContainer } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const { isDark } = (0, theme_provider_cjs_namespaceObject.useTheme)();
    const match = /language-(\w+)/.exec(className || '');
    const codeContent = extractTextFromChildren(children).replace(/\n$/, '');
    const language = match?.[1] || '';
    const isPreviewable = language === code_LANGUAGES.HTML && !disabledFeatures.htmlPreview;
    const [showPreview, setShowPreview] = external_react_default().useState(isPreviewable && !isStreaming);
    const [isModalOpen, setIsModalOpen] = external_react_default().useState(false);
    const codeFontStyles = {
        fontSize: spacing.compactMode ? apollo_core_default().FontFamily.FontMonoSSize : apollo_core_default().FontFamily.FontMonoMSize,
        fontFamily: spacing.compactMode ? apollo_core_default().FontFamily.FontMonoSFamily : apollo_core_default().FontFamily.FontMonoMFamily,
        lineHeight: spacing.compactMode ? apollo_core_default().FontFamily.FontMonoSLineHeight : apollo_core_default().FontFamily.FontMonoMLineHeight,
        height: spacing.compactMode ? apollo_core_default().FontFamily.FontMonoSLineHeight : apollo_core_default().FontFamily.FontMonoMLineHeight,
        fontWeight: spacing.compactMode ? apollo_core_default().FontFamily.FontMonoSWeight : apollo_core_default().FontFamily.FontMonoMWeight
    };
    const isInline = inline || !className && 'string' == typeof children && !children.includes('\n');
    if (isInline) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("code", {
        style: {
            color: 'var(--color-foreground)',
            padding: `0 ${apollo_core_default().Padding.PadS}`,
            background: 'var(--color-background-disabled)',
            borderRadius: apollo_core_default().Border.BorderRadiusM,
            display: 'inline-block',
            width: 'fit-content',
            ...codeFontStyles
        },
        ...props,
        children: children
    });
    if (match?.[1] === 'math') try {
        const html = external_katex_default().renderToString(String(children).replace(/\n$/, ''), {
            output: 'mathml',
            trust: false,
            strict: true,
            throwOnError: false
        });
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
            dangerouslySetInnerHTML: {
                __html: html
            }
        });
    } catch (e) {
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("pre", {
            style: {
                color: 'var(--color-foreground)'
            },
            children: String(children)
        });
    }
    const previewContent = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(PreviewIframe, {
        sandbox: "allow-scripts",
        srcDoc: codeContent,
        title: "HTML Preview"
    });
    const markdownContent = language ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(SyntaxHighlighterContainer, {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_syntax_highlighter_namespaceObject.Prism, {
            language: language,
            style: isDark ? prism_namespaceObject.oneDark : prism_namespaceObject.oneLight,
            customStyle: {
                margin: 0,
                fontSize: spacing.compactMode ? apollo_core_default().FontFamily.FontMonoSSize : apollo_core_default().FontFamily.FontMonoMSize,
                fontFamily: spacing.compactMode ? apollo_core_default().FontFamily.FontMonoSFamily : apollo_core_default().FontFamily.FontMonoMFamily,
                padding: apollo_core_default().Spacing.SpacingBase,
                whiteSpace: 'pre-wrap',
                wordWrap: 'break-word',
                overflowWrap: 'break-word'
            },
            wrapLongLines: true,
            lineProps: {
                style: {
                    wordWrap: 'break-word',
                    whiteSpace: 'pre-wrap'
                }
            },
            children: codeContent
        })
    }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(NonCodeContentContainer, {
        children: children
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(CodeBlockContainer, {
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(CodeBlockHeader, {
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.ApChip, {
                                label: language || _({
                                    id: 'autopilot-chat.message.code-block-language'
                                })
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                                style: {
                                    display: 'flex',
                                    gap: apollo_core_default().Spacing.SpacingMicro
                                },
                                children: [
                                    isPreviewable && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(action_button_cjs_namespaceObject.AutopilotChatActionButton, {
                                        iconName: showPreview ? 'code' : 'visibility',
                                        tooltip: showPreview ? _({
                                            id: 'autopilot-chat.message.code-block-code'
                                        }) : _({
                                            id: 'autopilot-chat.message.code-block-preview'
                                        }),
                                        onClick: ()=>{
                                            setShowPreview(!showPreview);
                                        }
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(action_button_cjs_namespaceObject.AutopilotChatActionButton, {
                                        iconName: "open_in_full",
                                        tooltip: _({
                                            id: 'autopilot-chat.message.expand'
                                        }),
                                        onClick: ()=>{
                                            setIsModalOpen(true);
                                        }
                                    }),
                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(action_button_cjs_namespaceObject.AutopilotChatActionButton, {
                                        iconName: "content_copy",
                                        tooltip: _({
                                            id: 'autopilot-chat.message.code-block-copy'
                                        }),
                                        onClick: ()=>{
                                            navigator.clipboard.writeText(codeContent);
                                        },
                                        "data-testid": "autopilot-chat-code-copy"
                                    })
                                ]
                            })
                        ]
                    }),
                    isPreviewable ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ContentContainer, {
                        children: showPreview ? previewContent : markdownContent
                    }) : markdownContent
                ]
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CustomModal, {
                open: isModalOpen,
                onClose: ()=>setIsModalOpen(false),
                container: portalContainer,
                keepMounted: true,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(ModalContainer, {
                    children: [
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CloseButton, {
                            size: "small",
                            onClick: ()=>setIsModalOpen(false),
                            "aria-label": _({
                                id: 'autopilot-chat.message.close'
                            }),
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Close_default(), {
                                sx: {
                                    width: apollo_core_default().Spacing.SpacingBase,
                                    height: apollo_core_default().Spacing.SpacingBase
                                }
                            })
                        }),
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ModalContent, {
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                                style: {
                                    flex: 1,
                                    overflow: isPreviewable && showPreview ? 'hidden' : 'auto',
                                    minHeight: 0,
                                    marginTop: apollo_core_default().Spacing.SpacingXs
                                },
                                children: isPreviewable && showPreview ? previewContent : markdownContent
                            })
                        })
                    ]
                })
            })
        ]
    });
});
exports.Code = __webpack_exports__.Code;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "Code"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
