/*!
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
@license Proprietary

This software is the confidential and proprietary property of Lenovo SSG BASD.
You shall not disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement.

--- Build Info ---
Version: 1.1.1
Build Time: 2026/2/26 17:21:00
Build Commit: 81e556ed827e2f043668506dae6fae8dbb9ef395
*/
import{keydownHandler as e}from"../../prosemirror-keymap/dist/index.js";import{Plugin as t,TextSelection as r,NodeSelection as n,Selection as o}from"../../prosemirror-state/dist/index.js";import{Fragment as i,Slice as s}from"../../prosemirror-model/dist/index.js";import{DecorationSet as l,Decoration as a}from"../../prosemirror-view/dist/index.js";class c extends o{constructor(e){super(e,e)}map(e,t){let r=e.resolve(t.map(this.head));return c.valid(r)?new c(r):o.near(r)}content(){return s.empty}eq(e){return e instanceof c&&e.head==this.head}toJSON(){return{type:"gapcursor",pos:this.head}}static fromJSON(e,t){if("number"!=typeof t.pos)throw new RangeError("Invalid input for GapCursor.fromJSON");return new c(e.resolve(t.pos))}getBookmark(){return new d(this.anchor)}static valid(e){let t=e.parent;if(t.isTextblock||!function(e){for(let t=e.depth;t>=0;t--){let r=e.index(t),n=e.node(t);if(0!=r)for(let e=n.child(r-1);;e=e.lastChild){if(0==e.childCount&&!e.inlineContent||u(e.type))return!0;if(e.inlineContent)return!1}else if(n.type.spec.isolating)return!0}return!0}(e)||!function(e){for(let t=e.depth;t>=0;t--){let r=e.indexAfter(t),n=e.node(t);if(r!=n.childCount)for(let e=n.child(r);;e=e.firstChild){if(0==e.childCount&&!e.inlineContent||u(e.type))return!0;if(e.inlineContent)return!1}else if(n.type.spec.isolating)return!0}return!0}(e))return!1;let r=t.type.spec.allowGapCursor;if(null!=r)return r;let n=t.contentMatchAt(e.index()).defaultType;return n&&n.isTextblock}static findGapCursorFrom(e,t,r=!1){e:for(;;){if(!r&&c.valid(e))return e;let o=e.pos,i=null;for(let r=e.depth;;r--){let n=e.node(r);if(t>0?e.indexAfter(r)<n.childCount:e.index(r)>0){i=n.child(t>0?e.indexAfter(r):e.index(r)-1);break}if(0==r)return null;o+=t;let s=e.doc.resolve(o);if(c.valid(s))return s}for(;;){let s=t>0?i.firstChild:i.lastChild;if(!s){if(i.isAtom&&!i.isText&&!n.isSelectable(i)){e=e.doc.resolve(o+i.nodeSize*t),r=!1;continue e}break}i=s,o+=t;let l=e.doc.resolve(o);if(c.valid(l))return l}return null}}}c.prototype.visible=!1,c.findFrom=c.findGapCursorFrom,o.jsonID("gapcursor",c);class d{constructor(e){this.pos=e}map(e){return new d(e.map(this.pos))}resolve(e){let t=e.resolve(this.pos);return c.valid(t)?new c(t):o.near(t)}}function u(e){return e.isAtom||e.spec.isolating||e.spec.createGapCursor}function p(){return new t({props:{decorations:w,createSelectionBetween:(e,t,r)=>t.pos==r.pos&&c.valid(r)?new c(r):null,handleClick:m,handleKeyDown:f,handleDOMEvents:{beforeinput:v}}})}const f=e({ArrowLeft:h("horiz",-1),ArrowRight:h("horiz",1),ArrowUp:h("vert",-1),ArrowDown:h("vert",1)});function h(e,t){const n="vert"==e?t>0?"down":"up":t>0?"right":"left";return function(e,o,i){let s=e.selection,l=t>0?s.$to:s.$from,a=s.empty;if(s instanceof r){if(!i.endOfTextblock(n)||0==l.depth)return!1;a=!1,l=e.doc.resolve(t>0?l.after():l.before())}let d=c.findGapCursorFrom(l,t,a);return!!d&&(o&&o(e.tr.setSelection(new c(d))),!0)}}function m(e,t,r){if(!e||!e.editable)return!1;let o=e.state.doc.resolve(t);if(!c.valid(o))return!1;let i=e.posAtCoords({left:r.clientX,top:r.clientY});return!(i&&i.inside>-1&&n.isSelectable(e.state.doc.nodeAt(i.inside)))&&(e.dispatch(e.state.tr.setSelection(new c(o))),!0)}function v(e,t){if("insertCompositionText"!=t.inputType||!(e.state.selection instanceof c))return!1;let{$from:n}=e.state.selection,o=n.parent.contentMatchAt(n.index()).findWrapping(e.state.schema.nodes.text);if(!o)return!1;let l=i.empty;for(let r=o.length-1;r>=0;r--)l=i.from(o[r].createAndFill(null,l));let a=e.state.tr.replace(n.pos,n.pos,new s(l,0,0));return a.setSelection(r.near(a.doc.resolve(n.pos+1))),e.dispatch(a),!1}function w(e){if(!(e.selection instanceof c))return null;let t=document.createElement("div");return t.className="ProseMirror-gapcursor",l.create(e.doc,[a.widget(e.selection.head,t,{key:"gapcursor"})])}export{c as GapCursor,p as gapCursor};