"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__, {
    createResourceSuggestion: ()=>createResourceSuggestion,
    ResourceMentionPluginKey: ()=>ResourceMentionPluginKey
});
const state_namespaceObject = require("@tiptap/pm/state");
const index_cjs_namespaceObject = require("../../../service/index.cjs");
const ResourceMentionPluginKey = new state_namespaceObject.PluginKey('resourceMention');
const RESOURCE_QUERY_START_PATTERN = /^[a-zA-Z0-9_./\\&]/;
function getCursorCoordinates(editor, pos) {
    const coords = editor.view.coordsAtPos(pos);
    return {
        top: coords.top,
        left: coords.left
    };
}
function createResourceSuggestion(callbacks, suppressRef) {
    return {
        pluginKey: ResourceMentionPluginKey,
        allowSpaces: true,
        allow: ({ state, range })=>{
            if (suppressRef.current) return false;
            const textFrom = range.from + 1;
            const textTo = range.to;
            const query = textFrom < textTo ? state.doc.textBetween(textFrom, textTo) : '';
            if (query.length > 0 && !RESOURCE_QUERY_START_PATTERN.test(query.charAt(0))) return false;
            if (query.includes(index_cjs_namespaceObject.CHAT_RESOURCE_MENTION_TERMINATOR)) return false;
            if (0 === query.length && textFrom < state.doc.resolve(textFrom).end()) {
                const nextChar = state.doc.textBetween(textFrom, textFrom + 1, '', '');
                const isSpecialChar = nextChar.length > 0 && ' ' !== nextChar && !RESOURCE_QUERY_START_PATTERN.test(nextChar);
                if (isSpecialChar) return false;
            }
            return true;
        },
        render: ()=>({
                onStart: (props)=>{
                    const coords = getCursorCoordinates(props.editor, props.range.from);
                    callbacks.onStart?.(props.range, coords);
                    callbacks.onQueryChange?.(props.query, props.range);
                },
                onUpdate: (props)=>{
                    callbacks.onQueryChange?.(props.query, props.range);
                },
                onExit: ()=>{
                    callbacks.onExit?.();
                }
            })
    };
}
exports.ResourceMentionPluginKey = __webpack_exports__.ResourceMentionPluginKey;
exports.createResourceSuggestion = __webpack_exports__.createResourceSuggestion;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ResourceMentionPluginKey",
    "createResourceSuggestion"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
