"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    citationPlugin: ()=>citationPlugin,
    contentPartsToMarkdown: ()=>contentPartsToMarkdown
});
const external_unist_util_visit_namespaceObject = require("unist-util-visit");
const index_cjs_namespaceObject = require("../../../../service/index.cjs");
function citationPlugin() {
    const citationStack = [];
    return (tree)=>{
        (0, external_unist_util_visit_namespaceObject.visit)(tree, 'text', (node, _index, parent)=>{
            if (!node?.value || !parent) return;
            const text = String(node.value);
            const pattern = /\[\[cite-start:([^\]]+)\]\]|\[\[cite-end\]\]/g;
            const newChildren = [];
            let lastIndex = 0;
            let match;
            while(null !== (match = pattern.exec(text))){
                const before = text.slice(lastIndex, match.index);
                if (before) newChildren.push({
                    type: 'text',
                    value: before
                });
                const token = match[0];
                if (token.startsWith('[[cite-start:')) try {
                    const raw = atob(match[1]);
                    const decoded = raw.startsWith('%5B') ? decodeURIComponent(raw) : raw;
                    const citationsData = JSON.parse(decoded);
                    citationStack.push({
                        citationsData
                    });
                    newChildren.push({
                        type: 'element',
                        tagName: 'span',
                        data: {
                            hName: 'span',
                            hProperties: {
                                className: [
                                    index_cjs_namespaceObject.CHAT_CITATION_START
                                ],
                                'data-citation-ids': citationsData.map((c)=>String(c.id)).join(' '),
                                style: 'display:none'
                            }
                        },
                        children: []
                    });
                } catch (e) {
                    console.error('[CitationPlugin] Failed to parse citation data:', e);
                }
                else {
                    const info = citationStack.pop();
                    if (info) {
                        const citationComponents = info.citationsData.map((citation)=>({
                                type: 'citation',
                                data: {
                                    hName: 'citation',
                                    hProperties: {
                                        id: citation.id,
                                        title: citation.title,
                                        url: citation.url,
                                        page_number: citation.page_number,
                                        download_url: citation.download_url,
                                        messageId: citation.messageId
                                    }
                                }
                            }));
                        newChildren.push(...citationComponents);
                    }
                }
                lastIndex = match.index + match[0].length;
            }
            if (lastIndex < text.length) newChildren.push({
                type: 'text',
                value: text.slice(lastIndex)
            });
            const nodeIndex = parent.children.indexOf(node);
            if (nodeIndex >= 0) parent.children.splice(nodeIndex, 1, ...newChildren);
        });
    };
}
function contentPartsToMarkdown(messageId, contentParts) {
    const markdown = contentParts.map((part, _index)=>{
        let text = part.text ?? '';
        if (part.citations && part.citations.length > 0) {
            const citationsData = part.citations.map((citation)=>({
                    id: citation.id,
                    title: citation.title,
                    url: 'url' in citation ? citation.url : void 0,
                    download_url: 'download_url' in citation ? citation.download_url : void 0,
                    page_number: 'page_number' in citation ? citation.page_number : void 0,
                    messageId
                }));
            const encodedData = btoa(encodeURIComponent(JSON.stringify(citationsData)));
            const endsWithUrl = /https?:\/\/\S+$/.test(text);
            text = `[[cite-start:${encodedData}]]${text}${endsWithUrl ? ' ' : ''}[[cite-end]]`;
        }
        return text;
    }).join('');
    return markdown;
}
exports.citationPlugin = __webpack_exports__.citationPlugin;
exports.contentPartsToMarkdown = __webpack_exports__.contentPartsToMarkdown;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "citationPlugin",
    "contentPartsToMarkdown"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
