import{EditorSelection as x,countColumn as w,Prec as Q,EditorState as U}from"@codemirror/state";import{keymap as _}from"@codemirror/view";import{Language as J,defineLanguageFacet as Y,syntaxTree as A,foldService as Z,LanguageSupport as E,foldNodeProp as N,indentNodeProp as W,languageDataProp as ee,indentUnit as te,LanguageDescription as O,ParseContext as re}from"@codemirror/language";import{CompletionContext as ne}from"@codemirror/autocomplete";import{parser as oe,MarkdownParser as ie,parseCode as le,GFM as se,Subscript as fe,Superscript as me,Emoji as ae}from"@lezer/markdown";import{html as ue,htmlCompletionSource as ce}from"@codemirror/lang-html";import{NodeProp as de}from"@lezer/common";const D=Y({commentTokens:{block:{open:"<!--",close:"-->"}}}),F=new de,q=oe.configure({props:[N.add(e=>!e.is("Block")||e.is("Document")||I(e)!=null||pe(e)?void 0:(i,n)=>({from:n.doc.lineAt(i.from).to,to:i.to})),F.add(I),W.add({Document:()=>null}),ee.add({Document:D})]});function I(e){let i=/^(?:ATX|Setext)Heading(\d)$/.exec(e.name);return i?+i[1]:void 0}function pe(e){return e.name=="OrderedList"||e.name=="BulletList"}function he(e,i){let n=e;for(;;){let t=n.nextSibling,r;if(!t||(r=I(t.type))!=null&&r<=i)break;n=t}return n.to}const ge=Z.of((e,i,n)=>{for(let t=A(e).resolveInner(n,-1);t&&!(t.from<i);t=t.parent){let r=t.type.prop(F);if(r==null)continue;let l=he(t,r);if(l>n)return{from:n,to:l}}return null});function v(e){return new J(D,e,[],"markdown")}const $=v(q),ke=q.configure([se,fe,me,ae,{props:[N.add({Table:(e,i)=>({from:i.doc.lineAt(e.from).to,to:e.to})})]}]),C=v(ke);function xe(e,i){return n=>{if(n&&e){let t=null;if(n=/\S*/.exec(n)[0],typeof e=="function"?t=e(n):t=O.matchLanguageName(e,n,!0),t instanceof O)return t.support?t.support.language.parser:re.getSkippingParser(t.load());if(t)return t.parser}return i?i.parser:null}}class y{constructor(i,n,t,r,l,f,a){this.node=i,this.from=n,this.to=t,this.spaceBefore=r,this.spaceAfter=l,this.type=f,this.item=a}blank(i,n=!0){let t=this.spaceBefore+(this.node.name=="Blockquote"?">":"");if(i!=null){for(;t.length<i;)t+=" ";return t}else{for(let r=this.to-this.from-t.length-this.spaceAfter.length;r>0;r--)t+=" ";return t+(n?this.spaceAfter:"")}}marker(i,n){let t=this.node.name=="OrderedList"?String(+R(this.item,i)[2]+n):"";return this.spaceBefore+t+this.type+this.spaceAfter}}function H(e,i){let n=[],t=[];for(let r=e;r;r=r.parent){if(r.name=="FencedCode")return t;(r.name=="ListItem"||r.name=="Blockquote")&&n.push(r)}for(let r=n.length-1;r>=0;r--){let l=n[r],f,a=i.lineAt(l.from),o=l.from-a.from;if(l.name=="Blockquote"&&(f=/^ *>( ?)/.exec(a.text.slice(o))))t.push(new y(l,o,o+f[0].length,"",f[1],">",null));else if(l.name=="ListItem"&&l.parent.name=="OrderedList"&&(f=/^( *)\d+([.)])( *)/.exec(a.text.slice(o)))){let m=f[3],s=f[0].length;m.length>=4&&(m=m.slice(0,m.length-4),s-=4),t.push(new y(l.parent,o,o+s,f[1],m,f[2],l))}else if(l.name=="ListItem"&&l.parent.name=="BulletList"&&(f=/^( *)([-+*])( {1,4}\[[ xX]\])?( +)/.exec(a.text.slice(o)))){let m=f[4],s=f[0].length;m.length>4&&(m=m.slice(0,m.length-4),s-=4);let c=f[2];f[3]&&(c+=f[3].replace(/[xX]/," ")),t.push(new y(l.parent,o,o+s,f[1],m,c,l))}}return t}function R(e,i){return/^(\s*)(\d+)(?=[.)])/.exec(i.sliceString(e.from,e.from+10))}function P(e,i,n,t=0){for(let r=-1,l=e;;){if(l.name=="ListItem"){let a=R(l,i),o=+a[2];if(r>=0){if(o!=r+1)return;n.push({from:l.from+a[1].length,to:l.from+a[0].length,insert:String(r+2+t)})}r=o}let f=l.nextSibling;if(!f)break;l=f}}function T(e,i){let n=/^[ \t]*/.exec(e)[0].length;if(!n||i.facet(te)!=" ")return e;let t=w(e,4,n),r="";for(let l=t;l>0;)l>=4?(r+=" ",l-=4):(r+=" ",l--);return r+e.slice(n)}const X=({state:e,dispatch:i})=>{let n=A(e),{doc:t}=e,r=null,l=e.changeByRange(f=>{if(!f.empty||!C.isActiveAt(e,f.from,-1)&&!C.isActiveAt(e,f.from,1))return r={range:f};let a=f.from,o=t.lineAt(a),m=H(n.resolveInner(a,-1),t);for(;m.length&&m[m.length-1].from>a-o.from;)m.pop();if(!m.length)return r={range:f};let s=m[m.length-1];if(s.to-s.spaceAfter.length>a-o.from)return r={range:f};let c=a>=s.to-s.spaceAfter.length&&!/\S/.test(o.text.slice(s.to));if(s.item&&c){let d=s.node.firstChild,h=s.node.getChild("ListItem","ListItem");if(d.to>=a||h&&h.to<a||o.from>0&&!/[^\s>]/.test(t.lineAt(o.from-1).text)){let u=m.length>1?m[m.length-2]:null,b,B="";u&&u.item?(b=o.from+u.from,B=u.marker(t,1)):b=o.from+(u?u.to:0);let S=[{from:b,to:a,insert:B}];return s.node.name=="OrderedList"&&P(s.item,t,S,-2),u&&u.node.name=="OrderedList"&&P(u.item,t,S),{range:x.cursor(b+B.length),changes:S}}else{let u=V(m,e,o);return{range:x.cursor(a+u.length+1),changes:{from:o.from,insert:u+e.lineBreak}}}}if(s.node.name=="Blockquote"&&c&&o.from){let d=t.lineAt(o.from-1),h=/>\s*$/.exec(d.text);if(h&&h.index==s.from){let u=e.changes([{from:d.from+h.index,to:d.to},{from:o.from+s.from,to:o.to}]);return{range:f.map(u),changes:u}}}let p=[];s.node.name=="OrderedList"&&P(s.item,t,p);let k=s.item&&s.item.from<o.from,g="";if(!k||/^[\s\d.)\-+*>]*/.exec(o.text)[0].length>=s.to)for(let d=0,h=m.length-1;d<=h;d++)g+=d==h&&!k?m[d].marker(t,1):m[d].blank(d<h?w(o.text,4,m[d+1].from)-g.length:null);let L=a;for(;L>o.from&&/\s/.test(o.text.charAt(L-o.from-1));)L--;return g=T(g,e),Le(s.node,e.doc)&&(g=V(m,e,o)+e.lineBreak+g),p.push({from:L,to:a,insert:e.lineBreak+g}),{range:x.cursor(L+g.length+1),changes:p}});return r?!1:(i(e.update(l,{scrollIntoView:!0,userEvent:"input"})),!0)};function K(e){return e.name=="QuoteMark"||e.name=="ListMark"}function Le(e,i){if(e.name!="OrderedList"&&e.name!="BulletList")return!1;let n=e.firstChild,t=e.getChild("ListItem","ListItem");if(!t)return!1;let r=i.lineAt(n.to),l=i.lineAt(t.from),f=/^[\s>]*$/.test(r.text);return r.number+(f?0:1)<l.number}function V(e,i,n){let t="";for(let r=0,l=e.length-2;r<=l;r++)t+=e[r].blank(r<l?w(n.text,4,e[r+1].from)-t.length:null,r<l);return T(t,i)}function we(e,i){let n=e.resolveInner(i,-1),t=i;K(n)&&(t=n.from,n=n.parent);for(let r;r=n.childBefore(t);)if(K(r))t=r.from;else if(r.name=="OrderedList"||r.name=="BulletList")n=r.lastChild,t=n.to;else break;return n}const j=({state:e,dispatch:i})=>{let n=A(e),t=null,r=e.changeByRange(l=>{let f=l.from,{doc:a}=e;if(l.empty&&C.isActiveAt(e,l.from)){let o=a.lineAt(f),m=H(we(n,f),a);if(m.length){let s=m[m.length-1],c=s.to-s.spaceAfter.length+(s.spaceAfter?1:0);if(f-o.from>c&&!/\S/.test(o.text.slice(c,f-o.from)))return{range:x.cursor(o.from+c),changes:{from:o.from+c,to:f}};if(f-o.from==c&&(!s.item||o.from<=s.item.from||!/\S/.test(o.text.slice(0,s.to)))){let p=o.from+s.from;if(s.item&&s.node.from<s.item.from&&/\S/.test(o.text.slice(s.from,s.to))){let k=s.blank(w(o.text,4,s.to)-w(o.text,4,s.from));return p==o.from&&(k=T(k,e)),{range:x.cursor(p+k.length),changes:{from:p,to:o.from+s.to,insert:k}}}if(p<f)return{range:x.cursor(p),changes:{from:p,to:f}}}}}return t={range:l}});return t?!1:(i(e.update(r,{scrollIntoView:!0,userEvent:"delete"})),!0)},z=[{key:"Enter",run:X},{key:"Backspace",run:j}],G=ue({matchClosingTags:!1});function be(e={}){let{codeLanguages:i,defaultCodeLanguage:n,addKeymap:t=!0,base:{parser:r}=$,completeHTMLTags:l=!0,htmlTagLanguage:f=G}=e;if(!(r instanceof ie))throw new RangeError("Base parser provided to `markdown` should be a Markdown parser");let a=e.extensions?[e.extensions]:[],o=[f.support,ge],m;n instanceof E?(o.push(n.support),m=n.language):n&&(m=n);let s=i||m?xe(i,m):void 0;a.push(le({codeParser:s,htmlParser:f.language.parser})),t&&o.push(Q.high(_.of(z)));let c=v(r.configure(a));return l&&o.push(c.data.of({autocomplete:Ae})),new E(c,o)}function Ae(e){let{state:i,pos:n}=e,t=/<[:\-\.\w\u00b7-\uffff]*$/.exec(i.sliceDoc(n-25,n));if(!t)return null;let r=A(i).resolveInner(n,-1);for(;r&&!r.type.isTop;){if(r.name=="CodeBlock"||r.name=="FencedCode"||r.name=="ProcessingInstructionBlock"||r.name=="CommentBlock"||r.name=="Link"||r.name=="Image")return null;r=r.parent}return{from:n-t[0].length,to:n,options:Ce(),validFor:/^<[:\-\.\w\u00b7-\uffff]*$/}}let M=null;function Ce(){if(M)return M;let e=ce(new ne(U.create({extensions:G}),0,!0));return M=e?e.options:[]}export{$ as commonmarkLanguage,j as deleteMarkupBackward,X as insertNewlineContinueMarkup,be as markdown,z as markdownKeymap,C as markdownLanguage};