import { PluginKey } from "@tiptap/pm/state";
import { CHAT_RESOURCE_MENTION_TERMINATOR } from "../../../service/index.js";
const ResourceMentionPluginKey = new 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(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?.();
                }
            })
    };
}
export { ResourceMentionPluginKey, createResourceSuggestion };
