/*!
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{canJoin as e,joinPoint as t,liftTarget as n,replaceStep as o,canSplit as r,findWrapping as l,ReplaceAroundStep as i,ReplaceStep as s}from"../../prosemirror-transform/dist/index.js";import{Slice as c,Fragment as f}from"../../prosemirror-model/dist/index.js";import{AllSelection as p,TextSelection as d,Selection as a,NodeSelection as u}from"../../prosemirror-state/dist/index.js";const h=(e,t)=>!e.selection.empty&&(t&&t(e.tr.deleteSelection().scrollIntoView()),!0);function m(e,t){let{$cursor:n}=e.selection;return!n||(t?!t.endOfTextblock("backward",e):n.parentOffset>0)?null:n}const b=(e,t,r)=>{let l=m(e,r);if(!l)return!1;let i=I(l);if(!i){let o=l.blockRange(),r=o&&n(o);return null!=r&&(t&&t(e.tr.lift(o,r).scrollIntoView()),!0)}let s=i.nodeBefore;if(P(e,i,t,-1))return!0;if(0==l.parent.content.size&&(x(s,"end")||u.isSelectable(s)))for(let n=l.depth;;n--){let r=o(e.doc,l.before(n),l.after(n),c.empty);if(r&&r.slice.size<r.to-r.from){if(t){let n=e.tr.step(r);n.setSelection(x(s,"end")?a.findFrom(n.doc.resolve(n.mapping.map(i.pos,-1)),-1):u.create(n.doc,i.pos-s.nodeSize)),t(n.scrollIntoView())}return!0}if(1==n||l.node(n-1).childCount>1)break}return!(!s.isAtom||i.depth!=l.depth-1)&&(t&&t(e.tr.delete(i.pos-s.nodeSize,i.pos).scrollIntoView()),!0)},g=(e,t,n)=>{let o=m(e,n);if(!o)return!1;let r=I(o);return!!r&&y(e,r,t)},w=(e,t,n)=>{let o=V(e,n);if(!o)return!1;let r=T(o);return!!r&&y(e,r,t)};function y(e,t,n){let r=t.nodeBefore,l=t.pos-1;for(;!r.isTextblock;l--){if(r.type.spec.isolating)return!1;let e=r.lastChild;if(!e)return!1;r=e}let i=t.nodeAfter,f=t.pos+1;for(;!i.isTextblock;f++){if(i.type.spec.isolating)return!1;let e=i.firstChild;if(!e)return!1;i=e}let p=o(e.doc,l,f,c.empty);if(!p||p.from!=l||p instanceof s&&p.slice.size>=f-l)return!1;if(n){let t=e.tr.step(p);t.setSelection(d.create(t.doc,l)),n(t.scrollIntoView())}return!0}function x(e,t,n=!1){for(let o=e;o;o="start"==t?o.firstChild:o.lastChild){if(o.isTextblock)return!0;if(n&&1!=o.childCount)return!1}return!1}const k=(e,t,n)=>{let{$head:o,empty:r}=e.selection,l=o;if(!r)return!1;if(o.parent.isTextblock){if(n?!n.endOfTextblock("backward",e):o.parentOffset>0)return!1;l=I(o)}let i=l&&l.nodeBefore;return!(!i||!u.isSelectable(i))&&(t&&t(e.tr.setSelection(u.create(e.doc,l.pos-i.nodeSize)).scrollIntoView()),!0)};function I(e){if(!e.parent.type.spec.isolating)for(let t=e.depth-1;t>=0;t--){if(e.index(t)>0)return e.doc.resolve(e.before(t+1));if(e.node(t).type.spec.isolating)break}return null}function V(e,t){let{$cursor:n}=e.selection;return!n||(t?!t.endOfTextblock("forward",e):n.parentOffset<n.parent.content.size)?null:n}const $=(e,t,n)=>{let r=V(e,n);if(!r)return!1;let l=T(r);if(!l)return!1;let i=l.nodeAfter;if(P(e,l,t,1))return!0;if(0==r.parent.content.size&&(x(i,"start")||u.isSelectable(i))){let n=o(e.doc,r.before(),r.after(),c.empty);if(n&&n.slice.size<n.to-n.from){if(t){let o=e.tr.step(n);o.setSelection(x(i,"start")?a.findFrom(o.doc.resolve(o.mapping.map(l.pos)),1):u.create(o.doc,o.mapping.map(l.pos))),t(o.scrollIntoView())}return!0}}return!(!i.isAtom||l.depth!=r.depth-1)&&(t&&t(e.tr.delete(l.pos,l.pos+i.nodeSize).scrollIntoView()),!0)},S=(e,t,n)=>{let{$head:o,empty:r}=e.selection,l=o;if(!r)return!1;if(o.parent.isTextblock){if(n?!n.endOfTextblock("forward",e):o.parentOffset<o.parent.content.size)return!1;l=T(o)}let i=l&&l.nodeAfter;return!(!i||!u.isSelectable(i))&&(t&&t(e.tr.setSelection(u.create(e.doc,l.pos)).scrollIntoView()),!0)};function T(e){if(!e.parent.type.spec.isolating)for(let t=e.depth-1;t>=0;t--){let n=e.node(t);if(e.index(t)+1<n.childCount)return e.doc.resolve(e.after(t+1));if(n.type.spec.isolating)break}return null}const A=(n,o)=>{let r,l=n.selection,i=l instanceof u;if(i){if(l.node.isTextblock||!e(n.doc,l.from))return!1;r=l.from}else if(r=t(n.doc,l.from,-1),null==r)return!1;if(o){let e=n.tr.join(r);i&&e.setSelection(u.create(e.doc,r-n.doc.resolve(r).nodeBefore.nodeSize)),o(e.scrollIntoView())}return!0},z=(n,o)=>{let r,l=n.selection;if(l instanceof u){if(l.node.isTextblock||!e(n.doc,l.to))return!1;r=l.to}else if(r=t(n.doc,l.to,1),null==r)return!1;return o&&o(n.tr.join(r).scrollIntoView()),!0},C=(e,t)=>{let{$from:o,$to:r}=e.selection,l=o.blockRange(r),i=l&&n(l);return null!=i&&(t&&t(e.tr.lift(l,i).scrollIntoView()),!0)},R=(e,t)=>{let{$head:n,$anchor:o}=e.selection;return!(!n.parent.type.spec.code||!n.sameParent(o))&&(t&&t(e.tr.insertText("\n").scrollIntoView()),!0)};function v(e){for(let t=0;t<e.edgeCount;t++){let{type:n}=e.edge(t);if(n.isTextblock&&!n.hasRequiredAttrs())return n}return null}const B=(e,t)=>{let{$head:n,$anchor:o}=e.selection;if(!n.parent.type.spec.code||!n.sameParent(o))return!1;let r=n.node(-1),l=n.indexAfter(-1),i=v(r.contentMatchAt(l));if(!i||!r.canReplaceWith(l,l,i))return!1;if(t){let o=n.after(),r=e.tr.replaceWith(o,o,i.createAndFill());r.setSelection(a.near(r.doc.resolve(o),1)),t(r.scrollIntoView())}return!0},O=(e,t)=>{let n=e.selection,{$from:o,$to:r}=n;if(n instanceof p||o.parent.inlineContent||r.parent.inlineContent)return!1;let l=v(r.parent.contentMatchAt(r.indexAfter()));if(!l||!l.isTextblock)return!1;if(t){let n=(!o.parentOffset&&r.index()<r.parent.childCount?o:r).pos,i=e.tr.insert(n,l.createAndFill());i.setSelection(d.create(i.doc,n+1)),t(i.scrollIntoView())}return!0},j=(e,t)=>{let{$cursor:o}=e.selection;if(!o||o.parent.content.size)return!1;if(o.depth>1&&o.after()!=o.end(-1)){let n=o.before();if(r(e.doc,n))return t&&t(e.tr.split(n).scrollIntoView()),!0}let l=o.blockRange(),i=l&&n(l);return null!=i&&(t&&t(e.tr.lift(l,i).scrollIntoView()),!0)};function M(e){return(e,t)=>{let{$from:n,$to:o}=e.selection;if(e.selection instanceof u&&e.selection.node.isBlock)return!(!n.parentOffset||!r(e.doc,n.pos))&&(t&&t(e.tr.split(n.pos).scrollIntoView()),!0);if(!n.depth)return!1;let l,i,s=[],c=!1,f=!1;for(let r=n.depth;;r--){if(n.node(r).isBlock){c=n.end(r)==n.pos+(n.depth-r),f=n.start(r)==n.pos-(n.depth-r),i=v(n.node(r-1).contentMatchAt(n.indexAfter(r-1))),s.unshift(c&&i?{type:i}:null),l=r;break}if(1==r)return!1;s.unshift(null)}let a=e.tr;(e.selection instanceof d||e.selection instanceof p)&&a.deleteSelection();let h=a.mapping.map(n.pos),m=r(a.doc,h,s.length,s);if(m||(s[0]=i?{type:i}:null,m=r(a.doc,h,s.length,s)),!m)return!1;if(a.split(h,s.length,s),!c&&f&&n.node(l).type!=i){let e=a.mapping.map(n.before(l)),t=a.doc.resolve(e);i&&n.node(l-1).canReplaceWith(t.index(),t.index()+1,i)&&a.setNodeMarkup(a.mapping.map(n.before(l)),i)}return t&&t(a.scrollIntoView()),!0}}const F=M(),W=(e,t)=>{let n,{$from:o,to:r}=e.selection,l=o.sharedDepth(r);return 0!=l&&(n=o.before(l),t&&t(e.tr.setSelection(u.create(e.doc,n))),!0)};function P(t,o,r,l){let s,p,d=o.nodeBefore,u=o.nodeAfter,h=d.type.spec.isolating||u.type.spec.isolating;if(!h&&function(t,n,o){let r=n.nodeBefore,l=n.nodeAfter,i=n.index();return!(!(r&&l&&r.type.compatibleContent(l.type))||(!r.content.size&&n.parent.canReplace(i-1,i)?(o&&o(t.tr.delete(n.pos-r.nodeSize,n.pos).scrollIntoView()),0):!n.parent.canReplace(i,i+1)||!l.isTextblock&&!e(t.doc,n.pos)||(o&&o(t.tr.join(n.pos).scrollIntoView()),0)))}(t,o,r))return!0;let m=!h&&o.parent.canReplace(o.index(),o.index()+1);if(m&&(s=(p=d.contentMatchAt(d.childCount)).findWrapping(u.type))&&p.matchType(s[0]||u.type).validEnd){if(r){let n=o.pos+u.nodeSize,l=f.empty;for(let e=s.length-1;e>=0;e--)l=f.from(s[e].create(null,l));l=f.from(d.copy(l));let p=t.tr.step(new i(o.pos-1,n,o.pos,n,new c(l,1,0),s.length,!0)),a=p.doc.resolve(n+2*s.length);a.nodeAfter&&a.nodeAfter.type==d.type&&e(p.doc,a.pos)&&p.join(a.pos),r(p.scrollIntoView())}return!0}let b=u.type.spec.isolating||l>0&&h?null:a.findFrom(o,1),g=b&&b.$from.blockRange(b.$to),w=g&&n(g);if(null!=w&&w>=o.depth)return r&&r(t.tr.lift(g,w).scrollIntoView()),!0;if(m&&x(u,"start",!0)&&x(d,"end")){let e=d,n=[];for(;n.push(e),!e.isTextblock;)e=e.lastChild;let l=u,s=1;for(;!l.isTextblock;l=l.firstChild)s++;if(e.canReplace(e.childCount,e.childCount,l.content)){if(r){let e=f.empty;for(let t=n.length-1;t>=0;t--)e=f.from(n[t].copy(e));r(t.tr.step(new i(o.pos-n.length,o.pos+u.nodeSize,o.pos+s,o.pos+u.nodeSize-s,new c(e,n.length,0),0,!0)).scrollIntoView())}return!0}}return!1}function q(e){return function(t,n){let o=t.selection,r=e<0?o.$from:o.$to,l=r.depth;for(;r.node(l).isInline;){if(!l)return!1;l--}return!!r.node(l).isTextblock&&(n&&n(t.tr.setSelection(d.create(t.doc,e<0?r.start(l):r.end(l)))),!0)}}const D=q(-1),E=q(1);function N(e,t=null){return function(n,o){let{$from:r,$to:i}=n.selection,s=r.blockRange(i),c=s&&l(s,e,t);return!!c&&(o&&o(n.tr.wrap(s,c).scrollIntoView()),!0)}}function G(e,t=null){return function(n,o){let r=!1;for(let l=0;l<n.selection.ranges.length&&!r;l++){let{$from:{pos:o},$to:{pos:i}}=n.selection.ranges[l];n.doc.nodesBetween(o,i,(o,l)=>{if(r)return!1;if(o.isTextblock&&!o.hasMarkup(e,t))if(o.type==e)r=!0;else{let t=n.doc.resolve(l),o=t.index();r=t.parent.canReplaceWith(o,o+1,e)}})}if(!r)return!1;if(o){let r=n.tr;for(let o=0;o<n.selection.ranges.length;o++){let{$from:{pos:l},$to:{pos:i}}=n.selection.ranges[o];r.setBlockType(l,i,e,t)}o(r.scrollIntoView())}return!0}}function H(...e){return function(t,n,o){for(let r=0;r<e.length;r++)if(e[r](t,n,o))return!0;return!1}}H(h,b,k),H(h,$,S),H(R,O,j,F),"undefined"!=typeof navigator?/Mac|iP(hone|[oa]d)/.test(navigator.platform):"undefined"!=typeof os&&os.platform&&os.platform();export{H as chainCommands,O as createParagraphNear,h as deleteSelection,B as exitCode,b as joinBackward,z as joinDown,$ as joinForward,g as joinTextblockBackward,w as joinTextblockForward,A as joinUp,C as lift,j as liftEmptyBlock,R as newlineInCode,k as selectNodeBackward,S as selectNodeForward,W as selectParentNode,E as selectTextblockEnd,D as selectTextblockStart,G as setBlockType,F as splitBlock,M as splitBlockAs,N as wrapIn};