import { DOMSerializer } from 'prosemirror-model';
import { Plugin, PluginKey, TextSelection } from 'prosemirror-state';
import { Decoration, DecorationSet } from 'prosemirror-view';
import { isNodeSelection } from "../../../utils/selection.js";
import { pType } from "../../base/BaseSchema/index.js";
import { createPlaceholder } from "../Placeholder/index.js";
import { isGapCursorSelection } from "./GapCursorSelection.js";
import "./gapcursor.css";
const key = new PluginKey('gapCursorPlugin');
export const gapCursor = () => new Plugin({
    key,
    state: {
        init: () => false,
        apply: (_tr, _pluginState, _oldState, newState) => {
            const sel = newState.selection;
            return isGapCursorSelection(sel) || isNodeSelection(sel);
        },
    },
    view() {
        return {
            update(view) {
                const pluginState = key.getState(view.state);
                view.dom.classList.toggle('Prosemirror-hide-cursor', pluginState);
            },
        };
    },
    props: {
        handleKeyPress(view) {
            const { state, state: { selection: sel }, } = view;
            if (isGapCursorSelection(sel)) {
                // Replace GapCursorSelection with empty textblock before run all other handlers.
                // This should be done before all inputRules and other handlers, that handle text input.
                // Thus, entering text into a native textblock and into a "virtual" one – GapCursor – will be the same.
                const tr = state.tr.replaceSelectionWith(pType(state.schema).create());
                tr.setSelection(TextSelection.create(tr.doc, sel.pos + 1));
                view.dispatch(tr.scrollIntoView());
            }
            return false;
        },
        decorations: ({ doc, selection }) => {
            if (isGapCursorSelection(selection)) {
                const position = selection.head;
                return DecorationSet.create(doc, [
                    Decoration.widget(position, toDOM, {
                        key: 'gapcursor',
                        side: -1,
                    }),
                ]);
            }
            return null;
        },
    },
});
const toDOM = (view, getPos) => {
    const node = pType(view.state.schema).create();
    const element = DOMSerializer.fromSchema(view.state.schema).serializeNode(node);
    const placeholderDOM = createPlaceholder(node, null, true);
    if (placeholderDOM)
        element.appendChild(placeholderDOM);
    element.classList.add('g-md-gapcursor');
    element.addEventListener('mousedown', () => {
        const pos = getPos();
        if (pos !== undefined) {
            view.dispatch(view.state.tr.replaceSelectionWith(node));
        }
    });
    return element;
};
//# sourceMappingURL=gapcursor.js.map
