import{t as e}from"./react-9ZmE0_ty.js";import{t}from"./api-client-bbJLzRVE.js";var n=`ppm-custom-order`;function r(){try{let e=localStorage.getItem(n);return e?JSON.parse(e):null}catch{return null}}function i(e){try{localStorage.setItem(n,JSON.stringify(e))}catch{}}function a(e,t){if(!t)return[...e];let n=new Map(t.map((e,t)=>[e,t]));return[...e].sort((e,t)=>(n.get(e.name)??1/0)-(n.get(t.name)??1/0))}var o=e((e,n)=>({projects:[],activeProject:null,customOrder:r(),loading:!1,error:null,fetchProjects:async()=>{e({loading:!0,error:null});try{e({projects:await t.get(`/api/projects`),loading:!1})}catch(t){e({error:t instanceof Error?t.message:`Failed to fetch projects`,loading:!1})}},setActiveProject:t=>{e({activeProject:t})},addProject:async(r,i)=>{let a=await t.post(`/api/projects`,{path:r,name:i});await n().fetchProjects();let o=n().projects.find(e=>e.name===(i??a.name)||e.path===r);return o&&e({activeProject:o}),a},setProjectColor:async(e,r)=>{await t.patch(`/api/projects/${encodeURIComponent(e)}/color`,{color:r}),await n().fetchProjects()},moveProject:async(r,o)=>{let{projects:s,customOrder:c}=n(),l=a(s,c),u=l.findIndex(e=>e.name===r);if(u===-1)return;let d=o===`up`?u-1:u+1;if(d<0||d>=l.length)return;let f=l.map(e=>e.name);[f[u],f[d]]=[f[d],f[u]],i(f),await t.patch(`/api/projects/reorder`,{order:f}),e({customOrder:f})},reorderProjects:async n=>{i(n),e({customOrder:n}),await t.patch(`/api/projects/reorder`,{order:n}).catch(()=>{})},renameProject:async(e,r)=>{await t.patch(`/api/projects/${encodeURIComponent(e)}`,{name:r}),await n().fetchProjects()},deleteProject:async n=>{await t.del(`/api/projects/${encodeURIComponent(n)}`),e(e=>{let t=e.projects.filter(e=>e.name!==n),r=e.customOrder?e.customOrder.filter(e=>e!==n):null;return r&&i(r),{projects:t,customOrder:r,activeProject:e.activeProject?.name===n?t[0]??null:e.activeProject}})}}));export{o as n,a as t};