import{A as B,r as l,B as R,C as S}from"./index-Bwmlaq-e.js";const N=B("projects",()=>{const c=l([]),n=l(null),d=l([]),f=l([]),h=l([]),u=l(!1),i=l(null),a=S();async function j(){u.value=!0,i.value=null;try{const e=await a.get("/projects");c.value=e.projects}catch(e){throw i.value=e instanceof Error?e.message:"Failed to fetch projects",e}finally{u.value=!1}}async function w(e){u.value=!0,i.value=null;try{n.value=await a.get(`/projects/${e}`),await p(e)}catch(o){throw i.value=o instanceof Error?o.message:"Failed to fetch project",o}finally{u.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 i.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}),s=c.value.findIndex(F=>F.id===e);return s!==-1&&(c.value[s]=r),((v=n.value)==null?void 0:v.id)===e&&(n.value=r),r}async function _(e,o){var s;const t=await a.patch(`/projects/${e}`,o),r=c.value.findIndex(v=>v.id===e);return r!==-1&&(c.value[r]=t),((s=n.value)==null?void 0:s.id)===e&&(n.value=t),t}async function g(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 P(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 x(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 A(e,o,t=!0){const s=await fetch(`http://localhost:8000/api/v1/projects/${e}/export?format=${o}&include_metadata=${t}`,{headers:{Authorization:`Bearer ${R()}`},credentials:"include"});if(!s.ok)throw new Error("Export failed");return await s.blob()}return{projects:c,currentProject:n,folders:d,documents:f,treeNodes:h,loading:u,error:i,fetchProjects:j,fetchProject:w,fetchProjectTree:p,createProject:m,updateProject:$,patchProject:_,deleteProject:g,createFolder:P,deleteFolder:x,fetchProjectGraph:E,exportProject:A}});export{N as u};
