"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__, {
    resourceTokenPlugin: ()=>resourceTokenPlugin,
    stripResourceTokens: ()=>stripResourceTokens
});
const external_unist_util_visit_namespaceObject = require("unist-util-visit");
const RESOURCE_TOKEN_PATTERN = /\[\[resource-token:(.*?)\]\]/g;
const createTextNode = (value)=>({
        type: 'text',
        value
    });
const createResourceChipNode = (resource)=>({
        type: 'resource-chip',
        data: {
            hName: 'resource-chip',
            hProperties: {
                id: resource.id,
                type: resource.type,
                icon: resource.icon,
                displayName: resource.displayName
            }
        }
    });
const parseResourceData = (json)=>{
    try {
        const parsed = JSON.parse(json);
        if (!parsed?.id || !parsed?.type || !parsed?.displayName) return null;
        return parsed;
    } catch (e) {
        console.error('[ResourceTokenPlugin] Failed to parse resource token:', e);
        return null;
    }
};
function stripResourceTokens(text) {
    return text.replace(RESOURCE_TOKEN_PATTERN, (match, json)=>{
        const resource = parseResourceData(json);
        return resource?.displayName ?? match;
    });
}
function resourceTokenPlugin() {
    return (tree)=>{
        (0, external_unist_util_visit_namespaceObject.visit)(tree, 'text', (node, _index, parent)=>{
            if (!node.value || !parent) return;
            const text = node.value;
            const matches = [
                ...text.matchAll(RESOURCE_TOKEN_PATTERN)
            ];
            if (0 === matches.length) return;
            const newChildren = [];
            let lastIndex = 0;
            for (const match of matches){
                const matchIndex = match.index ?? 0;
                const before = text.slice(lastIndex, matchIndex);
                if (before) newChildren.push(createTextNode(before));
                const resourceData = match[1] ? parseResourceData(match[1]) : null;
                newChildren.push(resourceData ? createResourceChipNode(resourceData) : createTextNode(match[0]));
                lastIndex = matchIndex + match[0].length;
            }
            const remaining = text.slice(lastIndex);
            if (remaining) newChildren.push(createTextNode(remaining));
            const nodeIndex = parent.children.indexOf(node);
            if (nodeIndex >= 0) parent.children.splice(nodeIndex, 1, ...newChildren);
        });
    };
}
exports.resourceTokenPlugin = __webpack_exports__.resourceTokenPlugin;
exports.stripResourceTokens = __webpack_exports__.stripResourceTokens;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "resourceTokenPlugin",
    "stripResourceTokens"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
