import{Kt as e}from"./xY0c-UfG.js";import{m as t,p as n}from"./XcWejFm_.js";import{a as r,r as i,t as a}from"./C1trcC8M.js";import{n as o}from"./D8wZ2xul.js";import{t as s}from"./BZqoC7tF.js";var c=e({commentExtension:()=>p,refreshCommentsEffect:()=>l}),l=n.define(),u=t.define({create(){return a.none},update(e,t){let n=e.map(t.changes);for(let e of t.effects)e.is(l),e.is(d)&&(n=e.value.map(t.changes));return n},provide:e=>i.decorations.from(e)}),d=n.define();function f(e,t){let n=s.getForEntry(t.repo,t.entryType,t.entryId),r=e.state.doc.length,i=[],c=[];for(let e of n){if(e.deletedAt||e.commentId!==e.threadId||!e.anchorRelpos)continue;let n=o(t.ydoc,t.ytext,e.anchorRelpos,e.anchorQuote??``);if(n.orphaned)continue;let s=Math.max(0,Math.min(n.from,r)),l=Math.max(s,Math.min(n.to,r));if(s===l)continue;let u=e.resolvedAt!==null,d=u?`sg-comment-highlight sg-comment-resolved`:`sg-comment-highlight`;i.push({from:s,to:l,deco:a.mark({class:d,attributes:{"data-comment-id":e.commentId,"data-thread-id":e.threadId}})}),c.push({commentId:e.commentId,threadId:e.threadId,from:s,to:l,recovered:n.recovered,resolved:u,authorRole:e.authorRole})}return i.sort((e,t)=>e.from-t.from||e.to-t.to),{decorations:a.set(i.map(e=>e.deco.range(e.from,e.to))),markers:c}}function p(e){let t=[];return[u,r.define(n=>{function r(){let r=e.getContext();if(!r)return;let{decorations:i,markers:a}=f(n,r);n.dispatch({effects:d.of(i)}),t=a,e.onMarkersChanged?.(a)}let i=!1,a=()=>{i||(i=!0,queueMicrotask(()=>{i=!1,r()}))};return a(),{update(e){for(let t of e.transactions)for(let e of t.effects)if(e.is(l)){a();return}e.docChanged&&a()}}}),i.domEventHandlers({click(n,r){let i=n.target.closest(`.sg-comment-highlight`);if(!i)return e.onCommentClickAway?.(),!1;let a=i.dataset.threadId;if(!a)return!1;if(t.some(e=>e.threadId===a))return e.onCommentClick(a),!0;let o=r.posAtCoords({x:n.clientX,y:n.clientY});if(o!==null){let n=null;for(let e of t)o>=e.from&&o<=e.to&&(!n||e.to-e.from<n.to-n.from)&&(n=e);if(n)return e.onCommentClick(n.threadId),!0}return e.onCommentClickAway?.(),!1}})]}export{l as n,c as t};