"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__, {
    TipTapEditor: ()=>TipTapEditor
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const extension_document_namespaceObject = require("@tiptap/extension-document");
var extension_document_default = /*#__PURE__*/ __webpack_require__.n(extension_document_namespaceObject);
const extension_hard_break_namespaceObject = require("@tiptap/extension-hard-break");
var extension_hard_break_default = /*#__PURE__*/ __webpack_require__.n(extension_hard_break_namespaceObject);
const extension_mention_namespaceObject = require("@tiptap/extension-mention");
var extension_mention_default = /*#__PURE__*/ __webpack_require__.n(extension_mention_namespaceObject);
const extension_paragraph_namespaceObject = require("@tiptap/extension-paragraph");
var extension_paragraph_default = /*#__PURE__*/ __webpack_require__.n(extension_paragraph_namespaceObject);
const extension_text_namespaceObject = require("@tiptap/extension-text");
var extension_text_default = /*#__PURE__*/ __webpack_require__.n(extension_text_namespaceObject);
const extensions_namespaceObject = require("@tiptap/extensions");
const model_namespaceObject = require("@tiptap/pm/model");
const react_namespaceObject = require("@tiptap/react");
const suggestion_namespaceObject = require("@tiptap/suggestion");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const external_resource_chip_node_view_cjs_namespaceObject = require("./resource-chip-node-view.cjs");
const external_tiptap_utils_cjs_namespaceObject = require("./tiptap.utils.cjs");
const external_tiptap_editor_styles_cjs_namespaceObject = require("./tiptap-editor.styles.cjs");
const external_tiptap_resource_suggestion_cjs_namespaceObject = require("./tiptap-resource-suggestion.cjs");
const ResourceMention = extension_mention_default().extend({
    addAttributes () {
        return {
            ...this.parent?.(),
            type: {
                parseHTML: (element)=>element.getAttribute('data-resource-type'),
                renderHTML: (attributes)=>attributes.type ? {
                        'data-resource-type': attributes.type
                    } : {}
            },
            icon: {
                parseHTML: (element)=>element.getAttribute('data-icon'),
                renderHTML: (attributes)=>attributes.icon ? {
                        'data-icon': attributes.icon
                    } : {}
            }
        };
    },
    addNodeView () {
        return (0, react_namespaceObject.ReactNodeViewRenderer)(external_resource_chip_node_view_cjs_namespaceObject.ResourceChipNodeView);
    }
});
function TipTapEditorInner(props, ref) {
    const { value = '', placeholder = '', minRows = 1, maxRows = 6, lineHeight = '20px', onChange, onKeyDown, onMentionStart, onMentionEnd, onMentionQueryChange } = props;
    const callbacksRef = external_react_default().useRef({
        onChange,
        onKeyDown,
        onMentionStart,
        onMentionEnd,
        onMentionQueryChange
    });
    const mentionRangeRef = external_react_default().useRef(null);
    const suppressSuggestionRef = external_react_default().useRef(false);
    external_react_default().useLayoutEffect(()=>{
        callbacksRef.current = {
            onChange,
            onKeyDown,
            onMentionStart,
            onMentionEnd,
            onMentionQueryChange
        };
    }, [
        onChange,
        onKeyDown,
        onMentionStart,
        onMentionEnd,
        onMentionQueryChange
    ]);
    const resourceSuggestion = external_react_default().useMemo(()=>(0, external_tiptap_resource_suggestion_cjs_namespaceObject.createResourceSuggestion)({
            onStart: (range, coords)=>{
                mentionRangeRef.current = range;
                callbacksRef.current.onMentionStart?.(range, coords);
            },
            onExit: ()=>{
                mentionRangeRef.current = null;
                callbacksRef.current.onMentionEnd?.();
            },
            onQueryChange: (query, range)=>{
                mentionRangeRef.current = range;
                callbacksRef.current.onMentionQueryChange?.(query, range);
            }
        }, suppressSuggestionRef), []);
    const extensions = external_react_default().useMemo(()=>[
            extension_document_default(),
            extension_paragraph_default(),
            extension_text_default(),
            extensions_namespaceObject.UndoRedo,
            extension_hard_break_default().configure({
                keepMarks: false
            }),
            extensions_namespaceObject.Placeholder.configure({
                placeholder,
                emptyEditorClass: 'is-editor-empty'
            }),
            ResourceMention.configure({
                deleteTriggerWithBackspace: true,
                suggestion: resourceSuggestion,
                renderText: ({ node })=>node.attrs.label ?? node.attrs.id
            })
        ], [
        placeholder,
        resourceSuggestion
    ]);
    const editor = (0, react_namespaceObject.useEditor)({
        extensions,
        coreExtensionOptions: {
            clipboardTextSerializer: {
                blockSeparator: '\n'
            }
        },
        content: value ? (0, external_tiptap_utils_cjs_namespaceObject.textToDocument)(value) : '',
        onUpdate: ({ editor: ed })=>{
            const text = ed.getText();
            if (0 === text.trim().length) ed.commands.clearContent();
            callbacksRef.current.onChange?.(ed.getText());
            ed.commands.scrollIntoView();
        },
        editorProps: {
            handleKeyDown: (_view, event)=>callbacksRef.current.onKeyDown?.(event) ?? false,
            handleDOMEvents: {
                mousedown: ()=>{
                    suppressSuggestionRef.current = false;
                    return false;
                },
                keydown: ()=>{
                    suppressSuggestionRef.current = false;
                    return false;
                }
            },
            attributes: {
                'data-testid': 'tiptap-editor'
            },
            clipboardTextParser: (text, context)=>{
                const doc = (0, external_tiptap_utils_cjs_namespaceObject.textToDocument)(text);
                const nodes = doc.content.map((para)=>model_namespaceObject.Node.fromJSON(context.doc.type.schema, para));
                const fragment = model_namespaceObject.Fragment.fromArray(nodes);
                return model_namespaceObject.Slice.maxOpen(fragment);
            }
        }
    });
    external_react_default().useEffect(()=>{
        if (!editor) return;
        const currentText = editor.getText();
        if (currentText === value) return;
        editor.commands.setContent(value ? (0, external_tiptap_utils_cjs_namespaceObject.textToDocument)(value) : '', {
            emitUpdate: false
        });
    }, [
        value,
        editor
    ]);
    const insertResource = (0, external_react_namespaceObject.useCallback)((resource, range)=>{
        if (!editor) return;
        const chain = editor.chain().focus();
        if (range) chain.deleteRange(range);
        chain.insertContent({
            type: 'mention',
            attrs: {
                id: resource.id,
                label: resource.displayName,
                type: resource.type,
                icon: resource.icon
            }
        }).insertContent({
            type: 'text',
            text: ' '
        }).run();
    }, [
        editor
    ]);
    const getSerializedContent = (0, external_react_namespaceObject.useCallback)(()=>{
        if (!editor) return '';
        try {
            return editor.getText({
                blockSeparator: '\n',
                textSerializers: {
                    mention: ({ node })=>{
                        const { id, label, type, icon } = node.attrs;
                        return `[[resource-token:${JSON.stringify({
                            id,
                            type,
                            displayName: label,
                            ...icon && {
                                icon
                            }
                        })}]]`;
                    },
                    hardBreak: ()=>'\n'
                }
            });
        } catch  {
            return editor.getText() || '';
        }
    }, [
        editor
    ]);
    const focus = (0, external_react_namespaceObject.useCallback)(()=>{
        editor?.commands.focus();
    }, [
        editor
    ]);
    const clear = (0, external_react_namespaceObject.useCallback)(()=>{
        editor?.commands.clearContent();
    }, [
        editor
    ]);
    const clearMentionQuery = (0, external_react_namespaceObject.useCallback)(()=>{
        if (!editor || !mentionRangeRef.current) return;
        const range = mentionRangeRef.current;
        const queryStart = range.from + 1;
        if (queryStart < range.to) {
            editor.chain().focus().deleteRange({
                from: queryStart,
                to: range.to
            }).run();
            mentionRangeRef.current = {
                from: range.from,
                to: queryStart
            };
        }
    }, [
        editor
    ]);
    const triggerMention = (0, external_react_namespaceObject.useCallback)(()=>{
        if (!editor) return;
        suppressSuggestionRef.current = false;
        editor.chain().focus().insertContent('@').run();
    }, [
        editor
    ]);
    const exitMention = (0, external_react_namespaceObject.useCallback)(()=>{
        if (!editor) return;
        suppressSuggestionRef.current = true;
        (0, suggestion_namespaceObject.exitSuggestion)(editor.view, external_tiptap_resource_suggestion_cjs_namespaceObject.ResourceMentionPluginKey);
    }, [
        editor
    ]);
    external_react_default().useImperativeHandle(ref, ()=>({
            focus,
            insertResource,
            getSerializedContent,
            clear,
            clearMentionQuery,
            triggerMention,
            exitMention
        }), [
        focus,
        insertResource,
        getSerializedContent,
        clear,
        clearMentionQuery,
        triggerMention,
        exitMention
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_tiptap_editor_styles_cjs_namespaceObject.EditorContainer, {
        minRows: minRows,
        maxRows: maxRows,
        lineHeight: lineHeight,
        onClick: focus,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(react_namespaceObject.EditorContent, {
            editor: editor
        })
    });
}
const TipTapEditor = /*#__PURE__*/ external_react_default().memo(/*#__PURE__*/ (0, external_react_namespaceObject.forwardRef)(TipTapEditorInner));
TipTapEditor.displayName = 'TipTapEditor';
exports.TipTapEditor = __webpack_exports__.TipTapEditor;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "TipTapEditor"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
