import{y as b,r as d,aI as W,c as N,aG as T}from"./index-DOFLUERq.js";import{api as M}from"./common-DEIQtiPI.js";const R=async e=>{const a=await M.call("jingrow.desk.desktop.get_desktop_page",{page:JSON.stringify(e)});return a.message||a},D=async()=>{try{const e=await M.call("jingrow.desk.desktop.get_workspace_sidebar_items"),a=e.message||e;return{pages:a.pages||[],has_access:a.has_access||!1,has_create_access:a.has_create_access||!1,is_local:a.is_local||!1}}catch(e){throw console.error("Failed to fetch workspace sidebar items:",e),new Error(e.message||"Failed to fetch workspace sidebar items")}},_=e=>{const a=e.public===0;return{id:e.name,key:e.name,label:e.title||e.name,type:"workspace",workspaceName:e.name,icon:e.icon||"tabler:layout-dashboard",isPrivate:a,parentId:e.parent_page||void 0,hidden:e.is_hidden===1,order:0}},j=e=>{const a=[],l=new Map;e.forEach(n=>{l.set(n.title,n.name)});const p=e.filter(n=>n.public===1),v=e.filter(n=>n.public===0);let m=1;return v.forEach(n=>{const u=_(n);n.parent_page&&(u.parentId=l.get(n.parent_page)||n.parent_page),u.order=m++,a.push(u)}),p.forEach(n=>{const u=_(n);n.parent_page&&(u.parentId=l.get(n.parent_page)||n.parent_page),u.order=m++,a.push(u)}),a},k="app.menu.items";function B(){try{const e=localStorage.getItem(k);if(!e)return[];const a=JSON.parse(e);return Array.isArray(a)?a:[]}catch{return[]}}function w(e){localStorage.setItem(k,JSON.stringify(e))}function y(){return[]}const G=b("menu",()=>{const e=d(!1);let a=null;const l=d({}),p=d(!1),v=d(!1),m=d(!1),n=d([]),o=d((()=>{const s=B();if(Array.isArray(s)&&s.length)return s;const t=y();return w(t),t})());function f(){const s=o.value.filter(r=>!r.parentId),t=o.value.filter(r=>r.parentId);s.sort((r,g)=>(r.order??0)-(g.order??0));const c=t.reduce((r,g)=>{const h=g.parentId;return r[h]||(r[h]=[]),r[h].push(g),r},{});Object.values(c).forEach(r=>{r.sort((g,h)=>(g.order??0)-(h.order??0))});const i=[];s.forEach(r=>{i.push(r),c[r.id]&&i.push(...c[r.id])}),w(i),o.value=i}function S(s){const t={...s,id:s.id||crypto.randomUUID()};if(t.order===void 0||t.order===null)if(t.parentId){const c=o.value.filter(r=>r.parentId===t.parentId),i=Math.max(...c.map(r=>r.order??0),0);t.order=i+1}else{const c=o.value.filter(r=>!r.parentId),i=Math.max(...c.map(r=>r.order??0),0);t.order=i+1}o.value.push(t),f()}function E(s,t){const c=o.value.findIndex(i=>i.id===s);c>=0&&(o.value[c]={...o.value[c],...t},f())}function x(s){o.value=o.value.filter(t=>t.id!==s),f()}function O(){o.value=y(),f()}function A(){localStorage.removeItem(k),o.value=y(),f()}async function I(){return a||(e.value?{success:!0,count:o.value.length}:(a=(async()=>{try{const s=await D(),t=j(s.pages),c={};for(const i of s.pages){const r=W(i.name);c[r]=i}return l.value=c,p.value=s.has_access||!1,v.value=s.has_create_access||!1,m.value=s.is_local||!1,o.value=t,f(),e.value=!0,{success:!0,count:t.length}}catch(s){return console.error("Failed to load menu from backend:",s),{success:!1,error:s.message}}finally{a=null}})(),a))}async function F(){return e.value=!1,a=null,I()}const P=N(()=>{const s=T();return o.value.filter(t=>{if(t.hidden||!m.value&&t.type==="workspace"&&t.label==="Local")return!1;if(t.type==="pagetype"&&t.pagetype){const c=t.pagetype;if(!s.canRead(c))return!1}return!0})});return{items:o,visibleItems:P,loadedFromBackend:e,workspaces:l,hasWorkspaceAccess:p,hasCreateWorkspaceAccess:v,isLocal:m,editableWorkspaceOrder:n,addMenu:S,updateMenu:E,removeMenu:x,resetDefault:O,clearCache:A,persist:f,loadFromBackend:I,reloadFromBackend:F}}),K=b("workspaceEdit",()=>{const e=d(!1);function a(){e.value=!0}function l(){e.value=!1}function p(){e.value=!e.value}return{isEditMode:e,enterEditMode:a,exitEditMode:l,toggleEditMode:p}});export{K as a,R as g,G as u};
