import{y as x,z as E,r as s}from"./index-BgrozaY3.js";const M=x("projects",()=>{const c=s([]),a=s(null),u=s([]),d=s([]),p=s([]),i=s(!1),l=s(null),r=E();async function j(){i.value=!0,l.value=null;try{const e=await r.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 y(e){i.value=!0,l.value=null;try{a.value=await r.get(`/projects/${e}`),await f(e)}catch(o){throw l.value=o instanceof Error?o.message:"Failed to fetch project",o}finally{i.value=!1}}async function f(e){try{const[o,t]=await Promise.all([r.get(`/projects/${e}/folders`),r.get(`/projects/${e}/documents`)]);u.value=o.folders,d.value=t.documents,m()}catch(o){throw l.value=o instanceof Error?o.message:"Failed to fetch project tree",o}}function m(){const e=new Map,o=[];for(const t of u.value){const n={id:t.id,name:t.name,type:"folder",children:[],parent_id:t.parent_id};e.set(t.id,n)}for(const t of d.value)if(!t.folder_id){const n={id:t.id,name:t.title,type:"document",parent_id:null};e.set(t.id,n)}for(const t of d.value)if(t.folder_id){const n=e.get(t.folder_id);n&&n.children&&n.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 n=e.get(t.parent_id);n&&n.children&&n.children.push(t)}p.value=o}async function w(e,o){const t=await r.post("/projects",{name:e,description:o});return c.value.push(t),t}async function _(e,o,t){var h;const n=await r.put(`/projects/${e}`,{name:o,description:t}),v=c.value.findIndex(F=>F.id===e);return v!==-1&&(c.value[v]=n),((h=a.value)==null?void 0:h.id)===e&&(a.value=n),n}async function g(e){var o;await r.delete(`/projects/${e}`),c.value=c.value.filter(t=>t.id!==e),((o=a.value)==null?void 0:o.id)===e&&(a.value=null)}async function P(e,o,t=null){const n=await r.post(`/projects/${e}/folders`,{name:o,parent_id:t});return u.value.push(n),await f(e),n}async function $(e){await r.delete(`/folders/${e}`),a.value&&await f(a.value.id)}return{projects:c,currentProject:a,folders:u,documents:d,treeNodes:p,loading:i,error:l,fetchProjects:j,fetchProject:y,fetchProjectTree:f,createProject:w,updateProject:_,deleteProject:g,createFolder:P,deleteFolder:$}});export{M as u};
