import{a4 as F,E as r,D as L,k as C}from"./BzhixHsn.js";import{u as y}from"./D01W4VS-.js";const A=F("conversations",()=>{const n=r([]),m=r(null),i=r(!1),s=r(null),{$api:c}=L(),{showError:E}=y(),f=r(1),h=r(20),l=r(0),d=r(!1),v=r(!1),S=C(()=>n.value.length),T=C(()=>n.value.length>0),P=C(()=>Math.ceil(l.value/h.value)),g=async(t=1,a=!1)=>{a?v.value=!0:i.value=!0,s.value=null;try{const e=new URLSearchParams({page:t.toString(),size:h.value.toString()}),o=await c(`/v1/conversations?${e}`);a?n.value.push(...o.items):n.value=o.items,f.value=o.page,l.value=o.total,d.value=o.pages>o.page}catch{s.value="Failed to fetch conversations"}finally{i.value=!1,v.value=!1}},$=async()=>{d.value&&!v.value&&await g(f.value+1,!0)},p=t=>{n.value.unshift(t),l.value+=1},M=async(t,a="New Conversation")=>{i.value=!0,s.value=null;try{const e=await c("/v1/conversations",{method:"POST",body:{assistant_id:t,title:a}});return p({id:e.id,title:e.title}),e}catch(e){const{showError:o}=y(),u=`Failed to create conversation: ${e instanceof Error?e.message:"Unknown error"}`;return s.value=u,o(u),null}finally{i.value=!1}},w=t=>{const a=n.value.length;n.value=n.value.filter(e=>e.id!==t),n.value.length<a&&(l.value=Math.max(0,l.value-1))};return{conversations:n,currentConversation:m,isLoading:i,isLoadingMore:v,error:s,conversationCount:S,hasConversations:T,currentPage:f,pageSize:h,totalCount:l,totalPages:P,hasNextPage:d,fetchConversations:g,loadMore:$,addConversation:p,createConversation:M,deleteConversation:w,removeConversation:async t=>{try{await c(`/v1/conversations/${t}`,{method:"DELETE"}),w(t)}catch(a){throw s.value="Failed to delete conversation",a}},updateConversationTitle:async(t,a)=>{try{const e=await c(`/v1/conversations/${t}`,{method:"PUT",body:{title:a}}),o=n.value.find(u=>u.id===t);return o&&(o.title=e.title),e}catch(e){throw s.value="Failed to update conversation",e}},generateTitle:async t=>{try{const a=await c(`/v1/conversations/${t}/generate-title`,{method:"POST"});if(a.title){const e=n.value.find(o=>o.id===t);e&&(e.title=a.title)}return a}catch(a){const e=`Failed to generate title: ${a instanceof Error?a.message:"Unknown error"}`;throw s.value=e,E(e),a}},refreshConversations:async()=>{await g(1)},setCurrentConversation:t=>{m.value=t},clearError:()=>{s.value=null}}});export{A as u};
