import{z as F,r as s,A as B,B as R}from"./index-D2fAAQJ2.js";const b=F("projects",()=>{const c=s([]),n=s(null),d=s([]),f=s([]),h=s([]),i=s(!1),l=s(null),a=R();async function j(){i.value=!0,l.value=null;try{const e=await a.get("/projects");c.value=e.projects}catch(e){throw l.value=e instanceof Error?e.message:"Failed to fetch projects",e}finally{i.value=!1}}async function w(e){i.value=!0,l.value=null;try{n.value=await a.get(`/projects/${e}`),await p(e)}catch(o){throw l.value=o instanceof Error?o.message:"Failed to fetch project",o}finally{i.value=!1}}async function p(e){try{const[o,t]=await Promise.all([a.get(`/projects/${e}/folders`),a.get(`/projects/${e}/documents`)]);d.value=o.folders,f.value=t.documents,y()}catch(o){throw l.value=o instanceof Error?o.message:"Failed to fetch project tree",o}}function y(){const e=new Map,o=[];for(const t of d.value){const r={id:t.id,name:t.name,type:"folder",children:[],parent_id:t.parent_id};e.set(t.id,r)}for(const t of f.value)if(!t.folder_id){const r={id:t.id,name:t.title,type:"document",parent_id:null};e.set(t.id,r)}for(const t of f.value)if(t.folder_id){const r=e.get(t.folder_id);r&&r.children&&r.children.push({id:t.id,name:t.title,type:"document",parent_id:t.folder_id})}for(const t of e.values())if(t.parent_id===null)o.push(t);else{const r=e.get(t.parent_id);r&&r.children&&r.children.push(t)}h.value=o}async function m(e,o){const t=await a.post("/projects",{name:e,description:o});return c.value.push(t),t}async function _(e,o,t){var v;const r=await a.put(`/projects/${e}`,{name:o,description:t}),u=c.value.findIndex(A=>A.id===e);return u!==-1&&(c.value[u]=r),((v=n.value)==null?void 0:v.id)===e&&(n.value=r),r}async function $(e){var o;await a.delete(`/projects/${e}`),c.value=c.value.filter(t=>t.id!==e),((o=n.value)==null?void 0:o.id)===e&&(n.value=null)}async function g(e,o,t=null){const r=await a.post(`/projects/${e}/folders`,{name:o,parent_id:t});return d.value.push(r),await p(e),r}async function P(e){await a.delete(`/folders/${e}`),n.value&&await p(n.value.id)}async function E(e,o=2){return await a.get(`/projects/${e}/graph?depth=${o}`)}async function x(e,o,t=!0){const u=await fetch(`http://localhost:8000/api/v1/projects/${e}/export?format=${o}&include_metadata=${t}`,{headers:{Authorization:`Bearer ${B()}`},credentials:"include"});if(!u.ok)throw new Error("Export failed");return await u.blob()}return{projects:c,currentProject:n,folders:d,documents:f,treeNodes:h,loading:i,error:l,fetchProjects:j,fetchProject:w,fetchProjectTree:p,createProject:m,updateProject:_,deleteProject:$,createFolder:g,deleteFolder:P,fetchProjectGraph:E,exportProject:x}});export{b as u};
