import{Kt as e,X as t,bt as n,ht as r,vt as i}from"./xY0c-UfG.js";import{Y as a}from"./BW-sf0N4.js";import{t as o}from"./vgzQgl7V.js";async function s(e,t,n,r){let i=r?.includeResolved===!1?`?includeResolved=false`:``,{comments:o}=await a(`/api/comments/${encodeURIComponent(e)}/${encodeURIComponent(t)}/${encodeURIComponent(n)}${i}`);return o}async function c(e,t,n,r){return a(`/api/comments/${encodeURIComponent(e)}/${encodeURIComponent(t)}/${encodeURIComponent(n)}`,{method:`POST`,body:JSON.stringify(r)})}async function l(e,t){let{comment:n}=await a(`/api/comments/${e}`,{method:`PUT`,body:JSON.stringify({body:t})});return n}async function u(e){let{comment:t}=await a(`/api/comments/${e}/resolve`,{method:`POST`});return t}async function d(e){let{comment:t}=await a(`/api/comments/${e}/reopen`,{method:`POST`});return t}async function f(e){let{comment:t}=await a(`/api/comments/${e}`,{method:`DELETE`});return t}var p=e({commentsStore:()=>F});function m(e,t,n){return`${e}/${t}/${n}`}function h(e){return m(e.repo,e.entryType,e.entryId)}var g=n(r({})),_=n(r({})),v=n(r({})),y=n(r({}));function b(e,n){let r=t(g)[e]??[],a=r.findIndex(e=>e.commentId===n.commentId),o=a>=0?[...r.slice(0,a),n,...r.slice(a+1)]:[...r,n];i(g,{...t(g),[e]:o},!0)}function x(e,n,r,a){let o=t(g)[e];if(!o)return;let s=o.map(e=>e.threadId===n?{...e,resolvedAt:r,resolvedByUserId:a}:e);i(g,{...t(g),[e]:s},!0)}function S(e,n){let r=t(g)[e];r&&i(g,{...t(g),[e]:r.filter(e=>e.commentId!==n)},!0)}function C(e){let n=h(e);if(t(v)[n])switch(e.type){case`comment_created`:case`comment_updated`:e.comment&&b(n,e.comment);return;case`comment_resolved`:{let t=e.comment;x(n,e.threadId,t?.resolvedAt??new Date().toISOString(),t?.resolvedByUserId??null);return}case`comment_reopened`:x(n,e.threadId,null,null);return;case`comment_deleted`:e.comment?b(n,e.comment):S(n,e.commentId);return}}o.addCommentHandler(C),o.setCommentAgentThinkingHandler(e=>{i(y,{...t(y),[e.threadId]:e.thinking},!0)}),o.addConnectHandler(()=>{for(let e of Object.keys(t(v))){if(!t(v)[e])continue;let[n,r,...i]=e.split(`/`);!n||!r||i.length===0||w(n,r,i.join(`/`))}});async function w(e,n,r){let a=m(e,n,r);if(!t(_)[a]){i(_,{...t(_),[a]:!0},!0);try{let o=await s(e,n,r,{includeResolved:!0});i(g,{...t(g),[a]:o},!0),i(v,{...t(v),[a]:!0},!0)}catch(e){e instanceof TypeError?console.debug(`[comments] load aborted (navigation/network) for`,a,e):console.warn(`[comments] load failed for`,a,e)}finally{i(_,{...t(_),[a]:!1},!0)}}}function T(e,n,r){return t(g)[m(e,n,r)]??[]}function E(e,n,r){return!!t(v)[m(e,n,r)]}function D(e,n,r){return!!t(_)[m(e,n,r)]}function O(e){let t=new Map;for(let n of e){let e=t.get(n.threadId);e||(e={head:null,replies:[]},t.set(n.threadId,e)),n.commentId===n.threadId?e.head=n:e.replies.push(n)}let n=[];for(let[e,r]of t.entries())r.head&&(r.replies.sort((e,t)=>e.createdAt.localeCompare(t.createdAt)),n.push({threadId:e,head:r.head,replies:r.replies,resolvedAt:r.head.resolvedAt}));return n.sort((e,t)=>e.head.createdAt.localeCompare(t.head.createdAt)),n}async function k(e,n,r,i){let{comment:a}=await c(e,n,r,i),o=m(e,n,r);return t(v)[o]&&b(o,a),a}async function A(e,n){let r=await l(e,n),i=m(r.repo,r.entryType,r.entryId);return t(v)[i]&&b(i,r),r}async function j(e){let t=await u(e);return x(m(t.repo,t.entryType,t.entryId),t.threadId,t.resolvedAt,t.resolvedByUserId),t}async function M(e){let t=await d(e);return x(m(t.repo,t.entryType,t.entryId),t.threadId,null,null),t}async function N(e){let n=await f(e),r=m(n.repo,n.entryType,n.entryId);return t(v)[r]&&b(r,n),n}function P(e){return!!t(y)[e]}var F={load:w,getForEntry:T,groupThreads:O,isLoaded:E,isLoading:D,isAgentThinking:P,create:k,edit:A,resolve:j,reopen:M,remove:N};export{p as n,F as t};