import{Kt as e,ht as t}from"./xY0c-UfG.js";import{a as n}from"./C1trcC8M.js";var r=t({open:!1,query:``,from:0,to:0,rect:null,view:null,selectedIdx:0});function i(){r.open=!1,r.query=``,r.rect=null,r.view=null,r.selectedIdx=0}var a=e({applyWikilinkPick:()=>l,wikilinkKeymap:()=>c,wikilinkPlugin:()=>s}),o=/\[\[([^\[\]\n]*)$/,s=n.fromClass(class{constructor(e){this.update({view:e,docChanged:!0,selectionSet:!0})}update(e){if(!e.docChanged&&!e.selectionSet&&!e.viewportChanged)return;let t=e.view,n=t.state.selection.main;if(n.from!==n.to){r.open&&i();return}let a=n.from,s=Math.max(0,a-256),c=t.state.sliceDoc(s,a),l=o.exec(c);if(!l){r.open&&i();return}let u=s+l.index+2,d=c.slice(l.index+2),f=!r.open||r.query!==d;r.open=!0,r.query=d,r.from=u,r.to=a,r.view=t,f&&(r.selectedIdx=0),queueMicrotask(()=>{if(!r.open)return;let e=t.coordsAtPos(r.to);e&&(r.rect={left:e.left,top:e.top,bottom:e.bottom})})}destroy(){r.open&&i()}}),c=[{key:`ArrowDown`,run:e=>r.open?(e.dom.dispatchEvent(new CustomEvent(`sg-wikilink-nav`,{detail:1})),!0):!1},{key:`ArrowUp`,run:e=>r.open?(e.dom.dispatchEvent(new CustomEvent(`sg-wikilink-nav`,{detail:-1})),!0):!1},{key:`Enter`,run:e=>r.open?(e.dom.dispatchEvent(new CustomEvent(`sg-wikilink-apply`)),!0):!1},{key:`Escape`,run:()=>r.open?(i(),!0):!1}];function l(e){let{view:t,from:n,to:a}=r;if(!t)return;let o=t.state.sliceDoc(a,Math.min(a+2,t.state.doc.length))!==`]]`,s=o?`${e}]]`:e,c=n+s.length+(o?0:2);t.dispatch({changes:{from:n,to:a,insert:s},selection:{anchor:c},userEvent:`input.complete`}),i(),t.focus()}export{r as i,a as n,i as r,l as t};